[英]Merge logging config of Spring boot and Logback
I have a following situation: A spring boot app using logback and several logging properties configured already in application.yml
我有以下情况:使用 logback 的 spring boot 应用程序和
application.yml
中已经配置的几个日志记录属性
I would like to add other config options that are only available via logback.xml
file but as soon as i add this file to the classpath (does not matter if it is named logback.xml
or logback-spring.xml
) it overrides everything from application.yml
and all logging levels/patterns and other options defined there stop having effects - it seems that after adding logback.xml
all options from appication.yml
are ignored and i would need to readd them in the xml file instead of yml.我想添加只能通过
logback.xml
文件获得的其他配置选项,但是只要我将此文件添加到类路径(不管它是命名为logback.xml
还是logback-spring.xml
),它就会覆盖所有来自application.yml
以及在那里定义的所有日志记录级别/模式和其他选项停止生效 - 添加logback.xml
后, appication.yml
中的所有选项都被忽略,我需要在 xml 文件而不是 yml 中读取它们。
My question is: is it possible to merge these two configs?我的问题是:是否可以合并这两个配置? I mean i only need to add one option to logback.xml and i do not want to force every other developer to learn logback.xml syntax while they are already familiar with
application.yml
way of configuring logging.我的意思是我只需要向 logback.xml 添加一个选项,并且我不想强迫每个其他开发人员学习 logback.xml 语法,而他们已经熟悉
application.yml
配置日志记录的方式。
UPDATE1更新1
One of things i am trying to do is to enable logback JMX access, therefore i create a logback-spring.xml
file with following contents:我想做的一件事是启用 logback JMX 访问,因此我创建了一个包含以下内容的
logback-spring.xml
文件:
<configuration debug="true">
<jmxConfigurator />
</configuration>
Right after i introduce this file i lose all logging from the webapp even if i have logging.level.root: info
in the application.yml
file.在我引入这个文件之后,即使我在
application.yml
文件中有logging.level.root: info
,我也会丢失 webapp 的所有日志记录。
I would also like to keep using root logging level and pattern as it is defined in the application.yml
.我还想继续使用在
application.yml
中定义的根日志记录级别和模式。 I also want to configure several turboFilters
which are specific to logback and, from my knowledge, can only be degined via logback configuration file.我还想配置几个特定于 logback 的
turboFilters
,据我所知,只能通过 logback 配置文件来定义。
UPDATE 2更新 2
Both @devatherock and @Ashish Patil asnwers helped me with the issue, i have been aware of the possibility to include spring configuration elements in logback.xml
via springProperty
but i have not seen it as a viable solution since i would have to rewrite multiple elements this way making the logback.xml
harder to read. @devatherock 和 @Ashish Patil asnwers 都帮助我解决了这个问题,我已经意识到可以通过
springProperty
在logback.xml
中包含弹簧配置元素,但我还没有将其视为可行的解决方案,因为我必须重写多个元素这种方式使logback.xml
更难阅读。
But as @devatherock mentioned - by default the config files ARE merged it is that logback requires the existence of appender and root logger in its configuration.但是正如@devatherock 所提到的 - 默认情况下,配置文件是合并的,它是 logback 需要在其配置中存在 appender 和 root 记录器。 Even when present in the
logback.xml
file the root logger level will still be overwritten by application.yml
setting (which is desired) but the pattern
property will not and we have to use springProperty
for that.即使存在于
logback.xml
文件中,根记录器级别仍将被application.yml
设置覆盖(这是所需的),但pattern
属性不会,我们必须为此使用springProperty
。
So while both answers were helpful but actually @devatherock's insignts were more helpful for me i am included to accept his answer.因此,虽然这两个答案都很有帮助,但实际上@devatherock 的 insignts 对我更有帮助,我也接受了他的回答。
I created a sample project, spring-boot-logback which specifies logging config in both logback.xml
and application.yml
我创建了一个示例项目spring-boot-logback ,它在
logback.xml
和application.yml
中指定日志配置
The root
log level gets set to WARN
in the logback.xml which looks like below: root
日志级别在 logback.xml 中设置为WARN
,如下所示:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>
There is a controller method that handles /hello
, with a DEBUG
and a INFO
log line.有一个处理
/hello
的控制器方法,带有一个DEBUG
和一个INFO
日志行。 The log level for the controller class is set to DEBUG
in the application.yml
file which looks like below:控制器类的日志级别在
application.yml
文件中设置为DEBUG
,如下所示:
logging:
level:
io.github.devatherock.demo.controller: DEBUG
I started the application and hit the /hello
controller endpoint.我启动了应用程序并点击了
/hello
控制器端点。 Below are the log lines that I saw:以下是我看到的日志行:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.7)
08:41:45.051 [http-nio-8080-exec-1] DEBUG i.g.d.d.controller.HelloController - Debug log in sayHello method
08:41:45.052 [http-nio-8080-exec-1] INFO i.g.d.d.controller.HelloController - Info log in sayHello method
None of the application startup logs are present because the root log level is set to WARN in the logback.xml.由于根日志级别在 logback.xml 中设置为 WARN,因此不存在任何应用程序启动日志。 Both the debug and info log lines from the controller are present because the controller log level has been set to DEBUG in the application.yml.
来自控制器的调试和信息日志行都存在,因为控制器日志级别已在 application.yml 中设置为 DEBUG。 Thus we can confirm that logging config from both logback.xml and application.yml are applied without any issues
因此,我们可以确认来自 logback.xml 和 application.yml 的日志记录配置均已应用,没有任何问题
Update 1:更新1:
Looks like if a logback.xml
is specified, at a minimum, it needs to have the appender, encoder and the attachment of appender to root log level, for the logging config overrides using spring boot's logging.*
properties to work.看起来如果指定了
logback.xml
,它至少需要有 appender、encoder 和 appender 到根日志级别的附件,以便使用 spring boot 的logging.*
属性覆盖日志配置。 Once I specified such a logback.xml
file with also the jmxConfigurator
, I was able to adjust the root and controller log levels in the application.yml
.一旦我用 jmxConfigurator 指定了这样的
logback.xml
文件,我就能够在jmxConfigurator
application.yml
调整根和控制器日志级别。 The new logback.xml
and application.yml
files below:新的
logback.xml
和application.yml
文件如下:
logback.xml with jmxConfigurator:带有 jmxConfigurator 的 logback.xml:
<configuration>
<jmxConfigurator />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
application.yml with root log level:具有根日志级别的 application.yml:
logging:
level:
root: WARN
io.github.devatherock.demo.controller: DEBUG
Use of springProperty might be suitable for your requirement.使用springProperty可能适合您的要求。
Let's say ,you have application.yml
like below:比方说,你有
application.yml
如下所示:
logging:
level:
root: DEBUG
... // other configs
Now you want to enforce configs of application.yml even after having logback.xml
, then you can add springProperty
in your logback.xml
:现在,即使在拥有
logback.xml
之后,您也想强制执行 application.yml 的配置,然后您可以在logback.xml
springProperty
<configuration>
<jmxConfigurator />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<springProperty scope="context" name="loggerProperty" source="logging.level.root"/>
<root level="${loggerProperty}">
<appender-ref ref="STDOUT" />
</root>
... //other configurations.
</configuration>
Now, here when you start your spring-boot, you can see application.yml's config value is getting read instead of overriding.现在,当您启动 spring-boot 时,您可以看到 application.yml 的配置值被读取而不是覆盖。
So by this way , you can have as many properties configured in logback
& in application.yml
.因此,通过这种方式,您可以在
logback
和application.yml
中配置尽可能多的属性。
Basically, you are referring Environment
properties (which are defined in your application.properties / application.yml) from your logback.基本上,您从 logback 中引用
Environment
属性(在 application.properties / application.yml 中定义)。
Here logging.level.root
is environment property which is defined application.yml
& you referred it in logback.xml
by giving it a name as loggerProperty
.这里
logging.level.root
是定义application.yml
的环境属性,您在logback.xml
中通过将其命名为loggerProperty
来引用它。 You are then using this loggerProperty
within your logback so that it will get used instead of logback's default one.然后,您将在 logback 中使用此
loggerProperty
,以便使用它而不是 logback 的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.