首页 文章

spring boot,logback和logging.config属性

提问于
浏览
46

我使用logback库实现了一个spring boot项目的日志记录 . 我想根据我的spring配置文件(属性'spring.pofiles.active')加载不同的日志配置文件 . 我有3个文件:logback-dev.xml,logback-inte.xml和logback-prod.xml . 我使用的是 Spring 季启动版本1.2.2.RELEASE .

正如您可以阅读spring boot文档(here) . 它说:

可以通过在类路径中包含相应的库来激活各种日志记录系统,并通过在类路径的根目录中提供合适的配置文件或在Spring Environment属性logging.config指定的位置进一步自定义 . (但请注意,由于在创建ApplicationContext之前初始化日志记录,因此无法在Spring @Configuration文件中控制@PropertySources的日志记录 . 系统属性和传统的Spring Boot外部配置文件工作正常 . )

所以我尝试在application.properties文件中设置'logging.config'属性:

logging.config=classpath:/logback-${spring.profiles.active}.xml

但是,当我启动我的应用程序时,我的logback- .xml未加载...

我认为日志记录是所有使用spring boot的项目遇到的常见问题 . 我想知道我是否在正确的方向,因为我有其他解决方案,但我发现它们不优雅(在logback.xml文件或命令行属性中使用Janino进行条件解析) .

6 回答

  • 62

    我找到了一个解决方案,我理解为什么spring不关心application.properties文件中定义的'logging.config'属性 .

    Solution and explanation :

    初始化日志记录时,spring boot仅查找类路径或环境变量(请参阅http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html) .

    我找到的最佳解决方案是包含一个父logback.xml文件,该文件将根据我的spring配置文件包含正确的日志配置文件 .

    logback.xml :

    <configuration>
        <include resource="logback-${spring.profiles.active}.xml"/>
    </configuration>
    

    logback-[profile].xml (在本例中为logback-dev.xml):

    <included>
    
        <!-- put your appenders -->
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
           <encoder>
               <pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern>
               <charset>utf8</charset>
            </encoder>
        </appender>
    
        <!-- put your loggers here -->
        <logger name="org.springframework.web" additivity="false" level="INFO">
            <appender-ref ref="CONSOLE" />
        </logger>
    
        <!-- put your root here -->
        <root level="warn">
            <appender-ref ref="CONSOLE" />
        </root>
    
    </included>
    

    启动应用程序时,必须在命令行参数中设置 Note : 'spring.profiles.active' . E.G for JVM属性: -Dspring.profiles.active=dev

    Ref docs :

    Edit (multiple active profiles) :为了避免多个文件,我们可以使用需要Janino依赖的条件处理(setup here),参见conditional documentation . 使用此方法,我们还可以同时检查多个活动配置文件 . E.G(我没有测试这个解决方案,如果它不起作用就发表评论):

    <configuration>
    
        <if condition='"${spring.profiles.active}".contains("profile1")'>
            <then>
             <!-- do whatever you want for profile1 -->
            </then>
        </if>
    
        <if condition='"${spring.profiles.active}".contains("profile2")'>
            <then>
             <!-- do whatever you want for profile2 -->
            </then>
        </if>
    
        <!-- common config -->
    
    </configuration>
    

    有关条件处理的另一个示例,请参阅javasenior答案 .

  • 24

    另一种可以处理多个配置文件的方法是为每个环境创建一个单独的属性文件 .

    application-prod.properties

    logging.config=classpath:logback-prod.xml
    

    application-dev.properties

    logging.config=classpath:logback-dev.xml
    

    application-local.properties

    logging.config=classpath:logback-local.xml
    

    BE AWARE

    如果你不小心,你最终可能会记录到意外的地方

    -Dspring.profiles.active=local,dev //will use logback-dev.xml
    -Dspring.profiles.active=dev,local //will use logback-local.xml
    
  • 9

    不是为每个配置文件添加单独的logback xmls或具有IF条件,我建议以下(如果你在xmls中的差异较小),以便于条件处理:

    <springProfile name="dev">
    <logger name="org.sample" level="DEBUG" />
    </springProfile>
    <springProfile name="prod">
    <logger name="org.sample" level="TRACE" />
    </springProfile>
    
  • 4

    使用logback进行条件处理将是一个没有很多logback文件的解决方案 . 这是a link和带 spring 配置文件的示例回溯配置 .

    <configuration>
    
        <property name="LOG_LEVEL" value="INFO"/>
    
            <if condition='"product".equals("${spring.profiles.active}")'>
               <then>
                    <property name="LOG_LEVEL" value="INFO"/>
               </then>
               <else>
                    <property name="LOG_LEVEL" value="ERROR"/>
               </else>
            </if>
    
             .
             .
             appender, logger tags etc.
             .
             .
    
             <root level="${LOG_LEVEL}">
                 <appender-ref ref="STDOUT"/>
             </root>
    
    </configuration>
    

    此外,您可能必须将此添加到您的pom.xml

    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>3.0.6</version>
    </dependency>
    
  • 0

    Spring在Logback XML文件中支持下一个标记 <springProperty/> ,这个标记描述了here . 这意味着您可以轻松地从Spring属性文件中添加变量,即使此变量值也可以通过Spring从环境/系统变量中解析出来 .

  • 3

    您可以为不同的配置文件指定不同的logback.xml,只需3个步骤:

    1,在 application.propertiesapplication.yml 中指定激活的配置文件:

    spring.profiles.active: test
    

    2,配置logback以按配置文件包含不同的配置:

    <!DOCTYPE configuration>
    <configuration scan="true" scanPeriod="30 seconds">
        <springProperty scope="context" name="profile" source="spring.profiles.active"/>
        <include resource="logback.${profile}.xml"/>
    </configuration>
    

    3,创建配置文件 logback.test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <included>
        <include resource="org/springframework/boot/logging/logback/base.xml"/>
        <root level="INFO"/>
    </included>
    

    这很简单,不需要做任何其他事情 .

相关问题