spring引导,logback和logging.config属性

我使用logback库实现了一个Spring启动项目的日志logging。 我想根据我的弹簧configuration文件(属性“spring.pofiles.active”)加载不同的日志loggingconfiguration文件。 我有3个文件:logback-dev.xml,logback-inte.xml和logback-prod.xml。 我正在使用弹簧启动版本1.2.2.RELEASE。

正如你可以在春季启动文档( 这里 )阅读。 它说:

可以通过在类path中包含适当的库来激活各种日志logging系统,并且可以通过在类path的根目录中或在Spring环境属性logging.config中指定的位置提供合适的configuration文件来进一步进行定制。 (注意,由于在创buildApplicationContext之前对日志进行了初始化,所以在Spring @Configuration文件中无法控制从@PropertySources进行的日志logging,系统属性和传统的Spring Boot外部configuration文件可以正常工作。

所以我试图在我的application.properties文件中设置“logging.config”属性:

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

但是,当我开始我的应用程序,我的logback- {configuration文件} .xml不加载…

我认为日志logging是所有使用spring boot的项目遇到的常见问题。 我想知道我是否在正确的方向,因为我也有其他的解决scheme,但我觉得他们不优雅(条件parsing与Janino在logback.xml文件或命令行属性)。

我find了一个解决scheme,我明白为什么spring不关心我在application.properties文件中定义的'logging.config'属性。

解决scheme和解释:

初始化日志logging时,Spring引导只能在类path或环境variables中查找(请参阅http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer .html )。

我发现最好的解决scheme是包含一个父级logback.xml文件,根据我的弹簧configuration文件将包含正确的日志configuration文件。

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> 

注意:在启动应用程序时,必须在命令行参数中设置“spring.profiles.active”。 EG用于JVM属性: -Dspring.profiles.active=dev

参考文件:

编辑(多个活动configuration文件) :为了避免多个文件,我们可以使用需要Janino依赖项的条件处理( 在此设置 ),请参阅条件文档 。 使用这种方法,我们也可以同时检查多个活动configuration文件。 EG(我没有testing这个解决scheme,如果不起作用,请发表评论):

 <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答案。

可以处理多个configuration文件的另一种方法是为每个环境创build一个单独的属性文件。

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 

意识到

如果你不小心,你可能会在某个意想不到的地方login

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

而不是为每个configuration文件添加单独的logback xmls或者具有IF条件,我会build议如下(如果您在xmls'中有较小的差异)进行简单的条件处理:

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

使用logback进行条件处理将是没有许多logback文件的解决scheme。 这是一个链接和弹簧configuration文件的示例logbackconfiguration。

 <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> 

Spring支持Logback XML文件中的下一个标签<springProperty/> ,这个标签在这里描述。 这意味着你可以很容易地从Spring属性文件中添加variables,即使这个variables值是由Spring的环境/系统variables来parsing的。