简体   繁体   English

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

[英]can we change the logging level of log4j at runtime

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

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

please help.. 请帮忙..

Calling the Logger.setLevel method with the desired Level can alter a Logger 's output level at runtime. 使用所需Level调用Logger.setLevel方法可以在运行时更改Logger的输出级别。

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. 上面演示了如何更改名为myLogger的一个Logger的级别,但是如果应该更改当前存储库中所有记录器的级别,则应调用Logger.getRootLogger获取的根记录器上的setLevel方法来更改级别在所有儿童记录器上。

The log level of a logger can be changed by calling setLevel as described by @coobird. 可以通过调用setLevel来更改记录器的日志级别,如@coobird所述。 However, there is a catch! 但是,有一个问题!

When you call getLogger(name) , the logging library will return you an existing Logger object if possible. 当您调用getLogger(name) ,如果可能,日志库将返回现有的Logger对象。 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. 如果其中一个线程调用setLevel ,则会更改所有其他线程的记录器级别。 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. 但是,我并不相信应用程序调用setLevel的智慧。 The setLevel method is about filtering the log messages, and you should not be wresting control of logging filtering away from the user / deployer. setLevel方法是关于过滤日志消息,您不应该从用户/部署者那里夺取对日志过滤的控制权。

I think it makes sense to call setLevel if a server has a "Controller" thread. 我认为如果服务器有一个“Controller”线程,调用setLevel是有意义的。 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. 但我不知道从一个单独的线程调用它会发生什么。

setLevel method is there only for java.util.logging.Logger and not for org.apache.logging.log4j.Logger setLevel方法只适用于java.util.logging.Logger而不适用于org.apache.logging.log4j.Logger

This is how we set log level in apache log4j 这是我们在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);

如果您使用的是Spring Boot(1.5+),则可以使用记录器端点来POST所需的日志记录级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM