我们可以在运行时更改 log4j 的日志记录级别吗

发布于 2024-10-07 17:49:33 字数 185 浏览 0 评论 0原文

我有一个问题,我想在运行时更改 log4j 的日志记录级别,我已经尝试了 log4j.properties 文件的很多操作,我还尝试编写一段代码,在特定时间后再次读取属性文件并再次配置记录器。

但问题是,我想将一个 API 调用的日志记录级别更改为 DEBUG,然后当该调用完成时,记录器应再次更改为之前的值..

请帮助..

i have an issue, i want to change the logging level of log4j at runtime, i have tried many things with log4j.properties file, i have also tried to written a code which after particular time again reads the properties file and again configure the logger.

but the problem is, i want to change the logging level to DEBUG for one API call, and then when that call is completed, the logger should again change to the previous value..

please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恋竹姑娘 2024-10-14 17:49:33

调用 Logger.setLevel 方法,具有所需的 Level 可以在运行时改变Logger 的输出级别。

下面是一个演示其用法的示例:

Logger logger = Logger.getLogger("myLogger");
logger.addAppender(new ConsoleAppender(new SimpleLayout()));

System.out.println("*** The current level will be INFO");

logger.setLevel(Level.INFO);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

System.out.println("*** Changing level to DEBUG");

// remember the previous level
Level previousLevel = logger.getLevel();

logger.setLevel(Level.DEBUG);
logger.warn("DEBUG and higher will appear");
logger.info("DEBUG and higher will appear");
logger.debug("DEBUG and higher will appear");

System.out.println("*** Changing level back to previous level");

// revert to previous level
logger.setLevel(previousLevel);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

上述输出:

*** The current level will be INFO
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear
*** Changing level to DEBUG
WARN - DEBUG and higher will appear
INFO - DEBUG and higher will appear
DEBUG - DEBUG and higher will appear
*** Changing level back to previous level
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear

上面演示了如何更改名为 myLogger 的一个 Logger 的级别,但如果所有记录器的级别应更改当前存储库中的 setLevel 方法,然后通过 Logger.getRootLogger 来更改所有子记录器的级别。

Calling the Logger.setLevel method with the desired Level can alter a Logger's output level at runtime.

The following is an example which demonstrates its usage:

Logger logger = Logger.getLogger("myLogger");
logger.addAppender(new ConsoleAppender(new SimpleLayout()));

System.out.println("*** The current level will be INFO");

logger.setLevel(Level.INFO);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

System.out.println("*** Changing level to DEBUG");

// remember the previous level
Level previousLevel = logger.getLevel();

logger.setLevel(Level.DEBUG);
logger.warn("DEBUG and higher will appear");
logger.info("DEBUG and higher will appear");
logger.debug("DEBUG and higher will appear");

System.out.println("*** Changing level back to previous level");

// revert to previous level
logger.setLevel(previousLevel);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

The above outputs:

*** The current level will be INFO
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear
*** Changing level to DEBUG
WARN - DEBUG and higher will appear
INFO - DEBUG and higher will appear
DEBUG - DEBUG and higher will appear
*** Changing level back to previous level
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear

The above demonstrates how to change the level of one Logger named myLogger, but if the levels of all the loggers in the current repository should be changed, then the setLevel method on the root logger obtained by Logger.getRootLogger should be called to change the levels on all the child loggers.

じее 2024-10-14 17:49:33

可以通过调用 setLevel 来更改记录器的日志级别,如@coobird所述。然而,有一个问题!

当您调用 getLogger(name) 时,日志记录库将返回一个现有的 Logger 对象(如果可能)。如果两个或多个线程请求具有相同名称的记录器,它们将获得相同的对象。如果其中一个线程调用 setLevel,这将更改所有其他线程的记录器级别。这可能会导致意外的行为。

如果您确实需要做这种事情,更好的方法是为您想要在不同级别进行日志记录的情况创建一个具有不同名称的记录器。

但是,我根本不相信应用程序调用 setLevel 是否明智。 setLevel 方法用于过滤日志消息,您不应该从用户/部署者手中夺取日志过滤的控制权。

The log level of a logger can be changed by calling setLevel as described by @coobird. However, there is a catch!

When you call getLogger(name), the logging library will return you an existing Logger object if possible. If two or more threads request a logger with the same name, they will get the same object. If one of the threads calls setLevel, this will change the logger level for all of the others. That can lead to unexpected behavior.

If you really need to do this kind of thing, a better approach would be to create a logger with a different name for the case where you want to logging at a different level.

However, I'm not convinced of the wisdom of the application calling setLevel at all. The setLevel method is about filtering the log messages, and you should not be wresting control of logging filtering away from the user / deployer.

一影成城 2024-10-14 17:49:33

我认为如果服务器有“控制器”线程,则调用 setLevel 是有意义的。这样,您可以在运行时动态更改日志记录级别以调试问题,并在完成后将其更改回来。

但我不知道从单独的线程调用它时会发生什么。

I think it makes sense to call setLevel if a server has a "Controller" thread. That way, you can dynamically change logging level at runtime to debug an issue, and change it back when you are done.

But I don't know what happens when it is called from a separate thread.

七色彩虹 2024-10-14 17:49:33

setLevel 方法仅适用于 java.util.logging.Logger,不适用于 org.apache.logging.log4j.Logger

这就是我们在 apache log4j 中设置日志级别的方法

org.apache.logging.log4j.core.LoggerContext
ctx = (LoggerContext) LogManager.getContext(false);
org.apache.logging.log4j.core.config.Configuration
conf = ctx.getConfiguration();
conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.DEBUG);
ctx.updateLoggers(conf);

setLevel method is there only for java.util.logging.Logger and not for org.apache.logging.log4j.Logger

This is how we set log level in apache log4j

org.apache.logging.log4j.core.LoggerContext
ctx = (LoggerContext) LogManager.getContext(false);
org.apache.logging.log4j.core.config.Configuration
conf = ctx.getConfiguration();
conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.DEBUG);
ctx.updateLoggers(conf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文