我们可以在运行时更改log4j的日志记录级别 [英] can we change the logging level of log4j at runtime

查看:132
本文介绍了我们可以在运行时更改log4j的日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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..

请帮助..

推荐答案

致电< a href =http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html#setLevel(org.apache.log4j.Level) =noreferrer> Logger.setLevel 方法,带有所需的 Level 可以在运行时更改 Logger 的输出级别。

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 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 方法应该调用/log4j/1.2/apidocs/org/apache/log4j/Logger.html#getRootLogger()\"rel =noreferrer> Logger.getRootLogger 改变所有c的水平hild loggers。

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.

这篇关于我们可以在运行时更改log4j的日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆