LG_*_*LG_ 53 java logging spring logback spring-boot
我使用logback库实现了一个spring boot项目的日志记录.我想根据我的spring配置文件(属性'spring.pofiles.active')加载不同的日志配置文件.我有3个文件:logback-dev.xml,logback-inte.xml和logback-prod.xml.我使用的是春季启动版本1.2.2.RELEASE.
正如您可以阅读spring boot文档(此处).它说:
可以通过在类路径中包含适当的库来激活各种日志记录系统,并通过在类路径的根目录中提供合适的配置文件或在Spring Environment属性logging.config指定的位置进一步自定义.(但请注意,由于在创建ApplicationContext之前初始化日志记录,因此无法在Spring @Configuration文件中控制@PropertySources的日志记录.系统属性和传统的Spring Boot外部配置文件工作正常.)
所以我尝试在application.properties文件中设置'logging.config'属性:
logging.config=classpath:/logback-${spring.profiles.active}.xml
Run Code Online (Sandbox Code Playgroud)
但是,当我启动我的应用程序时,我的logback- {profile} .xml未加载...
我认为日志记录是所有使用spring boot的项目遇到的常见问题.我想知道我是否在正确的方向,因为我有其他解决方案,但我觉得它们不优雅(在logback.xml文件或命令行属性中使用Janino进行条件解析).
LG_*_*LG_ 66
我找到了一个解决方案,我理解为什么spring不关心application.properties文件中定义的'logging.config'属性.
解决方案和解释:
初始化日志记录时,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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
注意:
启动应用程序时,必须在命令行参数中设置'spring.profiles.active'.EG for JVM属性:application.properties
参考文档:
编辑(多个活动配置文件):为了避免多个文件,我们可以使用需要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>
Run Code Online (Sandbox Code Playgroud)
有关条件处理的另一个示例,请参阅javasenior答案.
Zer*_*leb 27
另一种可以处理多个配置文件的方法是为每个环境创建一个单独的属性文件.
application-prod.properties
logging.config=classpath:logback-prod.xml
Run Code Online (Sandbox Code Playgroud)
application-dev.properties
logging.config=classpath:logback-dev.xml
Run Code Online (Sandbox Code Playgroud)
application-local.properties
logging.config=classpath:logback-local.xml
Run Code Online (Sandbox Code Playgroud)
意识到
如果你不小心你最终可能会记录到意外的地方
-Dspring.profiles.active=local,dev //will use logback-dev.xml
-Dspring.profiles.active=dev,local //will use logback-local.xml
Run Code Online (Sandbox Code Playgroud)
不是为每个配置文件添加单独的logback xmls或具有IF条件,我建议以下(如果你在xmls中的差异较小),以便于条件处理:
<springProfile name="dev">
<logger name="org.sample" level="DEBUG" />
</springProfile>
<springProfile name="prod">
<logger name="org.sample" level="TRACE" />
</springProfile>
Run Code Online (Sandbox Code Playgroud)
使用 logback 进行条件处理将是一个没有很多 logback 文件的解决方案。这是一个链接和带有 spring 配置文件的示例 logback 配置。
<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>
Run Code Online (Sandbox Code Playgroud)
此外,您可能必须将此添加到您的 pom.xml
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以为不同的配置文件指定不同的logback.xml,只需3步:
application.properties1、在或中指定激活的配置文件application.yml:
spring.profiles.active: test
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
非常简单,不需要做任何其他事情。
| 归档时间: |
|
| 查看次数: |
79809 次 |
| 最近记录: |