我们可以在运行时更改 log4j 的日志记录级别吗
我有一个问题,我想在运行时更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
调用
Logger.setLevel
方法,具有所需的Level
可以在运行时改变Logger
的输出级别。下面是一个演示其用法的示例:
上述输出:
上面演示了如何更改名为
myLogger
的一个Logger
的级别,但如果所有记录器的级别应更改当前存储库中的setLevel
方法,然后通过Logger.getRootLogger
来更改所有子记录器的级别。Calling the
Logger.setLevel
method with the desiredLevel
can alter aLogger
's output level at runtime.The following is an example which demonstrates its usage:
The above outputs:
The above demonstrates how to change the level of one
Logger
namedmyLogger
, but if the levels of all the loggers in the current repository should be changed, then thesetLevel
method on the root logger obtained byLogger.getRootLogger
should be called to change the levels on all the child loggers.可以通过调用
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 existingLogger
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 callssetLevel
, 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. ThesetLevel
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. 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 方法仅适用于 java.util.logging.Logger,不适用于 org.apache.logging.log4j.Logger
这就是我们在 apache log4j 中设置日志级别的方法
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
如果您使用的是 Spring Boot (1.5+),您可以使用记录器端点来发布所需的日志记录级别。
If you are using Spring Boot (1.5+), you can use logger endpoint to POST desired logging level.