log4j2中的层次结构无法正常工作

发布于 2025-01-23 04:21:47 字数 1725 浏览 0 评论 0原文

以下是类,log4j配置:

$ vi my.java

public class My {
    private static final Logger logger = LogManager.getLogger("foo.bar"); 
      
    public static void main(String[] args) {
        logger.trace("My trace");
        logger.debug("My debug");
        logger.info("My info");
        logger.warn("My warn");
        logger.error("My error");
...

$ vi log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="console" target="system_out">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </Console>
        <File name="foo" fileName="logs/foo.log">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </File>
        <File name="foo.bar" fileName="logs/foo.bar.log">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="console" />
        </Root>
        <Logger name="foo" level="info" additivity="false">
            <AppenderRef ref="foo" />
        </Logger>
        <Logger name="foo.bar" level="warn" additivity="false">
            <AppenderRef ref="foo.bar" />
        </Logger>
    </Loggers>
</Configuration>

in logs/foo.bar.log出现warn and code> and error messsages。这就是我所期望的。但是在logs/foo.log中什么都没看,我期望那里的消息消息。同样在控制台上什么也没有出现,但是我期望debug消息。因此,它无法正常工作。那我的错误是什么?

另外,logmanager.getlogger的参数必须是logger的名称。但是我也看到了该论点是一类的示例。它如何工作,因为当我这样做时,只有根记录器才能进行日志消息。

Below are a class and the log4j configuration:

$ vi My.java

public class My {
    private static final Logger logger = LogManager.getLogger("foo.bar"); 
      
    public static void main(String[] args) {
        logger.trace("My trace");
        logger.debug("My debug");
        logger.info("My info");
        logger.warn("My warn");
        logger.error("My error");
...

$ vi log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="console" target="system_out">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </Console>
        <File name="foo" fileName="logs/foo.log">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </File>
        <File name="foo.bar" fileName="logs/foo.bar.log">
            <PatternLayout pattern="%d %-5p %c : %m%n" />
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="console" />
        </Root>
        <Logger name="foo" level="info" additivity="false">
            <AppenderRef ref="foo" />
        </Logger>
        <Logger name="foo.bar" level="warn" additivity="false">
            <AppenderRef ref="foo.bar" />
        </Logger>
    </Loggers>
</Configuration>

In logs/foo.bar.log appears warn and error messsages. This is what I expected. But in logs/foo.log appears nothing, and I expected info messages there. Also on the console nothing appears, but I expected debug messages. So it does not work as I expected. So what is my mistake?

Also, the argument of LogManager.getLogger must be a name of a logger. But I also see examples where the argument is a class. How does that work, because when I do that only the root logger does log messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-01-30 04:21:47

log4j2中有两种继承:

  • logger配置的继承,也就是“ logger层次结构”,它将导致名为“ foo.bar.baz”的记录器使用其最近祖先的配置。酒吧”)。您的“ foo.bar”记录器使用您明确给它的配置。
  • 附录的继承,又称“添加性”,它将导致Logger配置继承其所有父的附录,除非您使用添加性=“ false”。在您的情况下,“ foo.bar”配置不使用“ foo”配置的附录。

配置的另一个问题是,“ foo.bar”记录器的级别设置为warn,因此它永远不会发送任何与warn不同的记录事件。和错误。如果您不想限制记录器本身,但是appender,则可以在&lt; appenderref&gt;上设置Level =“ Warn”

如果您的目的是将不同的日志级别拆分为不同的日志文件,则可以使用Level设置(基本上是阈值过滤器)和或a routing> routinging> routingappender):

<Logger name="foo.bar" level="DEBUG" additivity="false">
  <AppenderRef ref="foo.bar" level="WARN" />
  <AppenderRef ref="foo">
    <LevelMatchFilter level="INFO" />
  </AppenderRef>
  <AppenderRef ref="console">
    <LevelMatchFilter level="DEBUG" />
  </AppenderRef>
</Logger>

There are two kinds of inheritance in Log4j2:

  • inheritance of logger configuration, aka "logger hierarchy" that will cause a logger named "foo.bar.baz" to use the configuration of its nearest ancestor ("foo.bar"). Your "foo.bar" logger uses the configuration you gave it explicitly.
  • inheritance of appenders, aka "additivity" that will cause a logger config to inherit all of its parent's appenders, unless you use additivity="false". In your case the "foo.bar" configuration does not use the appenders of the "foo" configuration.

Another problem with your configuration is that the level of the "foo.bar" logger is set to WARN, so it will never send any logging event with a level different from WARN and ERROR. If you don't want to limit the logger itself, but the appender, you can set level="WARN" on the <AppenderRef>.

If your purpose is to split different log levels into different log files, you can use a mix of level settings (basically a threshold filter) and LevelMatchFilters (or a RoutingAppender):

<Logger name="foo.bar" level="DEBUG" additivity="false">
  <AppenderRef ref="foo.bar" level="WARN" />
  <AppenderRef ref="foo">
    <LevelMatchFilter level="INFO" />
  </AppenderRef>
  <AppenderRef ref="console">
    <LevelMatchFilter level="DEBUG" />
  </AppenderRef>
</Logger>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文