如何以编程方式更改 logback 的根日志记录级别
我有以下 logback.xml 文件:
<configuration debug="true">
<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="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
现在,在发生特定事件时,我想以编程方式将根记录器的级别从 debug 更改为 error。我不能使用变量替换,我必须在代码中执行此操作。
怎么办呢?谢谢。
I have the following logback.xml file:
<configuration debug="true">
<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="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Now, upon the occurrence of a specific event, I want to programmatically change the level of the root logger from debug to error. I can't use variable substitution, it is mandatory that I do this within the code.
How can it be done ? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
试试这个:
请注意,您还可以告诉 logback 定期扫描您的配置文件,如下所示:
Try this:
Note that you can also tell logback to periodically scan your config file like this:
使用 logback 1.1.3 我必须执行以下操作(Scala 代码):
using logback 1.1.3 I had to do the following (Scala code):
我假设您正在使用 logback (来自配置文件)。
从 logback 手册,我看到
Logger rootLogger = LoggerFactory.getLogger(org.slf4j .Logger.ROOT_LOGGER_NAME);
也许这可以帮助您更改该值?
I assume you are using logback (from the configuration file).
From logback manual, I see
Logger rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
Perhaps this can help you change the value?
我认为您可以使用 MDC 以编程方式更改日志记录级别。下面的代码是更改当前线程上的日志记录级别的示例。此方法不会创建对 logback 实现的依赖性(SLF4J API 包含 MDC)。
I think you can use MDC to change logging level programmatically. The code below is an example to change logging level on current thread. This approach does not create dependency to logback implementation (SLF4J API contains MDC).
正如其他人所指出的,您只需创建
mockAppender
,然后创建一个LoggingEvent
实例,该实例本质上侦听mockAppender
内注册/发生的日志记录事件。这是它在测试中的样子:
As pointed out by others, you simply create
mockAppender
and then create aLoggingEvent
instance which essentially listens to the logging event registered/happens insidemockAppender
.Here is how it looks like in test:
所以我基本上同意最上面的答案,但发现它在 2023 年略有不同。我发现以下工作
值得注意的主要区别是我必须使用
getILoggerFactory
而不是getLogger
代码>.要查看与此相关的其他帖子,请参阅以编程方式更改 Log4j2 中的日志级别 或者如果您希望每个请求都能执行此操作,请参阅 更改每个请求的 log4j 中的优先级So I mostly agree with the top answer but found it to be slightly different in 2023. I found that the following works
The primary difference of note is instead of
getLogger
I had to usegetILoggerFactory
. To see additional related posts to this see Programmatically change log level in Log4j2 or if you want to be able to this per request see Change priority level in log4j per request另一种方法是使用 Logback TurboFilter。这可以给我们更多的控制权。
更改记录器本身的级别只能让我们打开或关闭特定的记录器。如果我们想要为
user:123
或team:456
获取com.example.billing
中的所有内容,该怎么办? code> 模块在接下来的90 分钟
中,但留下来对其他一切都是WARN
?如果我们使用 TurboFilter,我们就可以访问 MDC,从中获取用户上下文。我们可以访问动态配置系统来获取要匹配的用户的规则。
这就是 https://github.com/prefab-cloud/prefab-cloud-java 使用 prefab.cloud 作为动态配置和 UI。
简化:
Another approach is to use a Logback TurboFilter. This can give us more control.
Changing the level of the logger itself only let's us turn a particular logger on or off. What if we want to get
DEBUG
foruser:123
orteam:456
for everything in thecom.example.billing
module for the next90 minutes
but stay areWARN
for everything else?If we use a TurboFilter, we have access to the MDC where we can get the user context. And we can access a dynamic config system to get the rules for which users to match.
This is what https://github.com/prefab-cloud/prefab-cloud-java does using prefab.cloud as the dynamic config and UI.
Simplified:
您可以通过 LogManager.getLogManager().updateConfiguration() 方法拦截日志记录配置。只需检查包含
.level
后缀的配置属性,并将默认值替换为Level.ALL
值。You may intercept logging configuration via the
LogManager.getLogManager().updateConfiguration()
method. Just check for configuration properties which contains.level
suffix, and replace default value withLevel.ALL
value.我似乎成功地做到了
然后从 netty 获取详细的日志记录,以下已经完成了
I seem to be having success doing
Then to get detailed logging from netty, the following has done it
这是一个控制器
}
Here's a controller
}