【问题标题】:logging.config configuration for spring boot春季启动的 logging.config 配置
【发布时间】:2015-09-19 06:54:58
【问题描述】:

我想在我的 Spring Boot 应用程序中配置 log4j.xml 文件的位置。 为此,我已将 logging.config 属性添加到我的 application.properties 配置中,指示 log4j.xml 文件路径。 但似乎这个属性被忽略了。 但它应该根据 Spring Boot 文档工作:

logging.config= # location of config file (default classpath:logback.xml for logback)

我是不是做错了什么?

【问题讨论】:

  • logging.config 的房产价值是多少?
  • 这是我的 logback.xml 的完整路径。

标签: spring logging log4j spring-boot logback


【解决方案1】:

Spring Boot 包含一些启动器,如果您想排除或交换特定的技术方面,可以使用这些启动器。它默认使用logback,如果你要使用log4j,请在你的类路径中添加spring-boot-starter-log4j。例如,使用 maven 会是这样的:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>1.2.4.RELEASE</version>
</dependency>

对于 log4j 1.x:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.2.4.RELEASE</version>
</dependency>

然后将logging.config 添加到您的application.properties

logging.config = classpath:path/to/log4j.xml

【讨论】:

    【解决方案2】:

    我发现在某些情况下,外部日志配置(logback.xml)不会被忽略:当应用程序从应用程序文件夹启动时,它可以正常工作。 关于这一点的一些澄清:应用程序是通过脚本运行的,可以从任何地方调用。 我还没有深入了解它为什么以这种方式工作,但是如果我在启动期间提供配置文件路径作为参数,它就会工作。所以我们只需将此参数添加到运行脚本中: --spring.config.location=/configPath/application.properties 可能这个问题是由 Spring 加载阶段引起的。 如果您知道这个问题的根本原因是什么,请分享:)

    【讨论】:

      【解决方案3】:

      根据spring boot docs

      如果您使用 starter pom 来组装依赖项,这意味着您必须排除 Logback,然后包含您选择的 Log4j 版本。

      像这样:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j</artifactId>
      </dependency>
      

      【讨论】:

        【解决方案4】:

        我花了几天时间来了解这是否应该有效,我对此表示怀疑。尽管文档中明确提到了如何使用Custom Log Configuration,但有些人将其视为differently。关于此属性的许多问题在 spring github 问题跟踪器上到处都不起作用,例如 thisthis。另一个有效点是,必须尽可能早地完成日志配置,以正确记录应用程序初始化。 因此系统属性在这里看起来是最精明的选项。您可以将其保存在您的应用程序代码中。唯一的要求是在 spring 上下文初始化之前设置它。

        @SpringBootApplication
        public class Application extends SpringBootServletInitializer {
        
            public static void main(String[] args) {
                // to start from command line
                System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
                SpringApplication.run(Application.class, args);
            }
        
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                // to start within container
                System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
                // this has SpringApplication::run() inside
                super.onStartup(servletContext);
            }
        }
        

        因为 Tomcat Web 容器中的所有应用程序都加载在同一个 JVM 中,所以处理自定义 logging.config 没有任何意义,而是对整个容器使用默认文件名的单个配置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-15
          • 2019-01-09
          • 1970-01-01
          • 2015-01-08
          • 2018-04-25
          • 2017-12-09
          • 2017-09-11
          • 2015-04-18
          相关资源
          最近更新 更多