Java spring boot、logback 和 logging.config 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29429073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring boot, logback and logging.config property
提问by LG_
I am implement logging in a spring boot project with logback library. I want to load different logging configuration files according to my spring profiles (property 'spring.pofiles.active'). I have 3 files : logback-dev.xml, logback-inte.xml and logback-prod.xml. I am using spring boot version 1.2.2.RELEASE.
我正在使用 logback 库实现登录 Spring Boot 项目。我想根据我的 spring 配置文件(属性“spring.pofiles.active”)加载不同的日志配置文件。我有 3 个文件:logback-dev.xml、logback-inte.xml 和 logback-prod.xml。我正在使用 Spring Boot 版本 1.2.2.RELEASE。
As you can read in spring boot documentation (here). It says:
正如您在 Spring Boot 文档(此处)中所读到的那样。它说:
The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config. (Note however that since logging is initialized before the ApplicationContext is created, it isn't possible to control logging from @PropertySources in Spring @Configuration files. System properties and the conventional Spring Boot external configuration files work just fine.)
可以通过在类路径中包含适当的库来激活各种日志系统,并通过在类路径的根目录中或在 Spring Environment 属性logging.config指定的位置提供合适的配置文件来进一步定制。(但是请注意,由于日志记录是在创建 ApplicationContext 之前初始化的,因此无法从 Spring @Configuration 文件中的 @PropertySources 控制日志记录。系统属性和传统的 Spring Boot 外部配置文件工作正常。)
So I tried to set 'logging.config' property in my application.properties file:
所以我尝试在我的 application.properties 文件中设置“logging.config”属性:
logging.config=classpath:/logback-${spring.profiles.active}.xml
But when i start my application, my logback-{profile}.xml is not loaded...
但是当我启动我的应用程序时,我的 logback-{profile}.xml 没有加载......
I think logging is a common problem that all projects using spring boot have encountered. Am I on the right track with the above approach? I have other solutions that work, but I find them not as elegant (conditional parsing with Janino in logback.xml file or command line property).
我认为日志记录是所有使用spring boot的项目都遇到的常见问题。通过上述方法,我是否走在正确的轨道上?我有其他可行的解决方案,但我发现它们并不优雅(在 logback.xml 文件或命令行属性中使用 Janino 进行条件解析)。
采纳答案by LG_
I found a solution and I understood why spring doesn't use my 'logging.config' property defined in the application.properties
file.
我找到了一个解决方案,我明白为什么 spring 不使用我在application.properties
文件中定义的“logging.config”属性。
Solution and explanation :
解决方法及说明:
When initializing logging, spring Boot only looks in classpath or environment variables.
初始化日志记录时,spring Boot 只在classpath 或 environment variables 中查找。
The solution I used was to include a parent logback.xml file that included the right logging config file according to the spring profile.
我使用的解决方案是包含一个父 logback.xml 文件,该文件根据 spring 配置文件包含正确的日志记录配置文件。
logback.xml :
logback.xml :
<configuration>
<include resource="logback-${spring.profiles.active}.xml"/>
</configuration>
logback-[profile].xml(in this case, logback-dev.xml) :
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' has to be set in command line arguments when starting the app.
E.G for JVM properties : -Dspring.profiles.active=dev
注意:启动应用程序时,必须在命令行参数中设置“spring.profiles.active”。用于 JVM 属性的 EG:-Dspring.profiles.active=dev
Ref docs :
参考文档:
- 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
- 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
Edit (multiple active profiles): In order to avoid multiple files, we could use conditional processing which requires Janino dependency (setup here), see conditional documentation. With this method, we can also check for multiple active profiles at the same time. E.G (I did not test this solution, so please comment if it does not work):
编辑(多个活动配置文件):为了避免多个文件,我们可以使用需要 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>
See javasenior answer for another example of a conditional processing.
有关条件处理的另一个示例,请参阅 javasenior 答案。
回答by Zergleb
Another approach that could handle multiple profiles is to create a separate properties file for each environment.
另一种可以处理多个配置文件的方法是为每个环境创建一个单独的属性文件。
application-prod.properties
application-prod.properties
logging.config=classpath:logback-prod.xml
application-dev.properties
应用程序-dev.properties
logging.config=classpath:logback-dev.xml
application-local.properties
应用程序-local.properties
logging.config=classpath:logback-local.xml
BE AWARE
意识到
If you aren't careful you could end up logging somewhere unexpected
如果你不小心,你最终可能会在意想不到的地方登录
-Dspring.profiles.active=local,dev //will use logback-dev.xml
-Dspring.profiles.active=dev,local //will use logback-local.xml
回答by javasenior
Conditional processing with logback will be a solution without many logback files. Here is a linkand a sample logback configuration with spring profiles.
使用 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>
Also, you might have to add this to your pom.xml
此外,您可能必须将此添加到您的 pom.xml
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
回答by nav3916872
Instead of adding separate logback xmls for each profile or having the IF condition , I would suggest the following (If you have less difference in the xmls') for easy conditional processing :
而不是为每个配置文件添加单独的 logback xmls 或具有 IF 条件,我会建议以下(如果您在 xmls 中的差异较小)以便于条件处理:
<springProfile name="dev">
<logger name="org.sample" level="DEBUG" />
</springProfile>
<springProfile name="prod">
<logger name="org.sample" level="TRACE" />
</springProfile>
回答by Andriy Rymar
Spring has support of next tag <springProperty/>
inside Logback XML file, this tag described here. It means that you can easily add variable from Spring property file, even this variable value resolves from environment/system variable by Spring.
Spring 支持<springProperty/>
Logback XML 文件中的 next 标签,这里描述了这个标签。这意味着您可以轻松地从 Spring 属性文件添加变量,即使此变量值由 Spring 从环境/系统变量解析。
回答by sulin
You can specific different logback.xml for different profile, only 3 steps:
您可以为不同的配置文件指定不同的 logback.xml,只需 3 步:
1, Specify actived profile in application.properties
or application.yml
:
1、在application.properties
或 中指定激活的配置文件application.yml
:
spring.profiles.active: test
2, Config logback to include different configuration by profile:
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, Create configuration file logback.test.xml
:
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>
It's very simple, don't need do anything else.
这很简单,不需要做任何其他事情。