Java Log4j 2 根记录器会覆盖所有内容吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19210527/
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-08-12 15:05:01  来源:igfitidea点击:

Log4j 2 root logger overrides everything?

javalogginglog4jlog4j2

提问by Danny Schweizer

I am relatively new to Log4j 2. Currently, I have this configuration file:

我对 Log4j 2 比较陌生。目前,我有这个配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <File name="DebugFile" fileName="../../logs/debug.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
    <File name="BenchmarkFile" fileName="../../logs/benchmark.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
  </Appenders>
  <Loggers> 
    <Logger name="com.messaging.main.ConsoleMain" level="debug">
      <AppenderRef ref="DebugFile"/>
    </Logger>
    <Logger name="com.messaging.main.ClientMain" level="debug">
      <AppenderRef ref="BenchmarkFile"/>
    </Logger>   
    <Root level="error">
      <AppenderRef ref="DebugFile"/>
    </Root>
  </Loggers>
</Configuration>

If I log something in these two classes ConsoleMain and ClientMain via a static Logger

如果我通过静态记录器在这两个类 ConsoleMain 和 ClientMain 中记录一些东西

    static Logger _logger = LogManager.getLogger(ClientMain.class.getName());

and

    static Logger _logger = LogManager.getLogger(ConsoleMain.class.getName());

they ALWAYS use the appender and level of the root logger. If the level of the root logger is "error" as above, it never shows any debug level logging output, even if the level of the individual loggers is debug. Also, it always appends to the log file specified in the root logger and not the one specified in the logger of the classes.

他们总是使用根记录器的附加程序和级别。如果根记录器的级别为上述“错误”,则它永远不会显示任何调试级别的日志输出,即使单个记录器的级别为调试。此外,它总是附加到根记录器中指定的日志文件,而不是类记录器中指定的日志文件。

So, it seems that the root logger somehow overrides everything. How do I get log4j to actually use the appender and the level of the loggers of the classes?

因此,似乎根记录器以某种方式覆盖了所有内容。如何让 log4j 实际使用 appender 和类的记录器级别?

I tried removing the appender of the root, but then it does not log anything.

我尝试删除根的附加程序,但它没有记录任何内容。

Thank you!

谢谢!

回答by Remko Popma

I tried your setup but I cannot reproduce the issue. Here is the code I used:

我尝试了您的设置,但无法重现该问题。这是我使用的代码:

package com.messaging.main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ClientMain {
    public static void main(String[] args) throws Exception {
        Logger logger = LogManager.getLogger(ClientMain.class);
        logger.debug("debug from ClientMain");
    }
}

package com.messaging.main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ConsoleMain {
    public static void main(String[] args) throws Exception {
        Logger logger = LogManager.getLogger(ConsoleMain.class);
        logger.debug("debug from ConsoleMain");
    }
}

when I run these with your exact config file I get the following output:

当我使用您的确切配置文件运行这些时,我得到以下输出:

benchmark.log:

基准.log:

07:59:51.070 [main] DEBUG com.messaging.main.ClientMain - debug from ClientMain

debug.log:

调试日志:

07:59:51.070 [main] DEBUG com.messaging.main.ClientMain - debug from ClientMain
07:59:58.306 [main] DEBUG com.messaging.main.ConsoleMain - debug from ConsoleMain
07:59:58.306 [main] DEBUG com.messaging.main.ConsoleMain - debug from ConsoleMain

This is expected behaviour. The duplicate entries are normal as by default additivity is true in log4j, so both the Root logger and the named Loggers will log the same message (see http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity). I'm not seeing the issue you are reporting that debug-level messages never appear in the log file when the Root level is "error".

这是预期的行为。重复条目是正常的,因为默认情况下 log4j 中的可加性为 true,因此根记录器和命名记录器都将记录相同的消息(请参阅http://logging.apache.org/log4j/2.x/manual/configuration。 html#Additivity)。我没有看到您报告的问题,即当根级别为“错误”时,调试级别的消息永远不会出现在日志文件中。

Perhaps something else is going on. What version of log4j2 are you using (most recent is now beta9)? Can you also try to reproduce the issue with the bare minimum sample code above and see if the issue still occurs?

也许还有其他事情正在发生。您使用的是什么版本的 log4j2(最近的版本是 beta9)?您是否也可以尝试使用上面的最低限度示例代码重现该问题,看看问题是否仍然存在?