java Log4j:以编程方式更改日志级别,适用于要创建的记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25506683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:08:56  来源:igfitidea点击:

Log4j: Change log level programmatically that works for to-be created loggers

javalog4j

提问by citress

I declare loggers in my code like this:

我在我的代码中声明记录器是这样的:

Logger logger = LoggerFactory.getLogger(MyClass.class);

I would like to be able to change the log level of my loggers programmatically at runtime. I have tried iterating over the loggers in the repository via the LogManager, but it only applies the new log level to loggers that are instantiated in the code. If a new logger instance is created, it does not use the new log level. My requirement is such that the log level in my web application needs to be configurable via an Administrative GUI, and this new log level needs to apply to all loggers in my code (exclude third party library logging, e.g. JSF, Spring, Hibernate etc).

我希望能够在运行时以编程方式更改记录器的日志级别。我曾尝试通过 LogManager 迭代存储库中的记录器,但它仅将新的日志级别应用于在代码中实例化的记录器。如果创建了一个新的记录器实例,它不会使用新的日志级别。我的要求是我的 web 应用程序中的日志级别需要通过管理 GUI 进行配置,并且这个新的日志级别需要应用于我代码中的所有记录器(不包括第三方库日志记录,例如 JSF、Spring、Hibernate 等) .

This is what I'm doing now which does not meet my requirements as it does not apply the log level to newly instantiated loggers:

这就是我现在正在做的事情,它不符合我的要求,因为它没有将日志级别应用于新实例化的记录器:

Enumeration<Logger> currentLoggers = LogManager.getCurrentLoggers();
while(currentLoggers.hasMoreElements()) {
    Logger logger = currentLoggers.nextElement();
    if (StringUtils.startsWith(logger.getName(), "com.my.company")) {
        logger.setLevel(logLevel);
    }
}

I am using Log4j 1.2.17.

我正在使用 Log4j 1.2.17。

回答by Fildor

Didn't find the resource anymore, but as to my faded memory LogManager.getLogger("com.my.company").setLevel(whateverloglevel)should do the job.

没有找到资源了,但至于我褪色的记忆LogManager.getLogger("com.my.company").setLevel(whateverloglevel)应该可以完成这项工作。

All loggers that are created with LogManager.getLogger(MyClass.class)where MyClass is in com.my.company or in a subtree of thatwill be affected.

使用LogManager.getLogger(MyClass.class)MyClass 在 com.my.company 中或在其子树中的位置创建的所有记录器都将受到影响。

whateverloglevelis one of Level:

whateverloglevel是以下级别之一

ALL
    The ALL has the lowest possible rank and is intended to turn on all logging.
DEBUG
    The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
ERROR
    The ERROR level designates error events that might still allow the application to continue running.
FATAL
    The FATAL level designates very severe error events that will presumably lead the application to abort.
INFO
    The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
OFF
    The OFF has the highest possible rank and is intended to turn off logging.
TRACE
    The TRACE Level designates finer-grained informational events than the DEBUG
WARN
    The WARN level designates potentially harmful situations.
全部
    ALL 具有尽可能低的等级,旨在打开所有日志记录。
调试
    调试级别指定对调试应用程序最有用的细粒度信息事件。
错误
    ERROR 级别指定可能仍允许应用程序继续运行的错误事件。
致命的
    FATAL 级别指定非常严重的错误事件,可能会导致应用程序中止。
信息
    INFO 级别指定信息性消息,在粗粒度级别突出显示应用程序的进度。
离开
    OFF 具有可能的最高等级,旨在关闭日志记录。
痕迹
    TRACE 级别指定比 DEBUG 更细粒度的信息事件
警告
    WARN 级别指定潜在的有害情况。