本文介绍了以编程方式更改Log4j2中的日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以编程方式更改Log4j2中的日志级别感兴趣。我试着查看他们的,但似乎没有有什么。我也尝试查看包: org.apache.logging.log4j.core.config ,但其中没有任何内容看起来也很有帮助。

I'm interested in programmatically changing the log level in Log4j2. I tried looking at their configuration documentation but that didn't seem to have anything. I also tried looking in the package: org.apache.logging.log4j.core.config, but nothing in there looked helpful either.

推荐答案

根据log4j2版本2.4常见问题解答

您可以设置记录器使用Log4j Core中的Configurator类进行操作。 但是请注意,Configurator类不是公共API的一部分。

You can set a logger’s level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API.

// org.apache.logging.log4j.core.config.Configurator;
Configurator.setLevel("com.example.Foo", Level.DEBUG);

// You can also set the root logger:
Configurator.setRootLevel(Level.DEBUG);

已编辑以反映Log4j2版本2.0.2中引入的API的变化

如果您想更改根记录器级别,请执行以下操作:

If you wish to change the root logger level, do something like this :

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(level);
ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.

是LoggerConfig的javadoc。

Here is the javadoc for LoggerConfig.

这篇关于以编程方式更改Log4j2中的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:34