我找到了解决方案,并且我理解了为什么spring不使用
application.properties文件中定义的’logging.config’属性。
解决方案和说明:
初始化日志记录时,spring boot只查找classpath或环境变量。
我使用的解决方案是包括一个父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" > <!-- enprers are assigned the type ch.qos.logback.classic.enprer.PatternLayoutEnprer by default --> <enprer><pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern><charset>utf8</charset> </enprer> </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 for
JVM属性:
-Dspring.profiles.active=dev
参考文档:
- http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
- http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
- http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html
编辑(多个活动配置文件)
:为了避免多个文件,我们可以使用需要Janino依赖的条件处理(在此处进行设置),请参阅条件文档。使用此方法,我们还可以同时检查多个活动配置文件。EG(我没有测试此解决方案,因此,如果它不起作用,请发表评论):
<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答案。



