简体   繁体   English

如何使用log4j2为不同的类使用不同的日志记录级别?

[英]How do i use different logging levels for different classes with log4j2?

In the log4j2 documentation log4j2 java configuration the default configuration is this: 在log4j2文档log4j2 java配置中 ,默认配置是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

This will produce logging for levels ERROR or FATAL to the Console. 这将为控制台生成ERROR或FATAL级别的日志记录。 My default logging works in the same way. 我的默认日志记录以相同的方式工作。 The problem i run into is when I do this, though: 我遇到的问题是当我这样做的时候:

<logger name="com.foo.Bar" level="TRACE"/>
<Root level="ERROR">
  <AppenderRef ref="STDOUT">
</Root>

According to the documentation, log4j2 java configuration , this will 根据文档, log4j2 java配置 ,这样就可以了

eliminate all the TRACE output from everything except com.foo.Bar. 消除除com.foo.Bar之外的所有TRACE输出。

My implementation, on the other hand, works as though the 另一方面,我的实现就好像是

<logger name="com.foo.Bar" level="TRACE"/>

wasn't even there. 甚至没有。 It will still only print out error logs. 它仍然只会打印错误日志。

Questions 问题

  • Has anyone else encountered this problem? 还有其他人遇到过这个问题吗?
  • Can anyone reproduce it? 任何人都可以复制它吗?
  • Does anyone know how to fix it? 有谁知道如何修理它? I almost would give someone a dollar. 我几乎会给别人一美元。 I would definitely settle for marking their answer green, though. 不过,我肯定会满足于将答案标记为绿色。

Here is some information about my setup: 以下是有关我的设置的一些信息:

Maven Dependencies Maven依赖

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
      </dependency>
      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-beta9</version>
      </dependency>
    <dependency>

Other 其他

I am testing this in the appropriate class. 我在适当的班级测试这个。 I am running a JUnit test file. 我正在运行JUnit测试文件。 I've used a src/main .xml file and i've used a src/test .xml file. 我使用了src / main .xml文件,并且我使用了src / test .xml文件。 Both end up with the same results. 两者最终都得到了相同的结果。

End Goal 结束目标

My end goal is to have a specific logger send SMTP requests. 我的最终目标是让特定的记录器发送SMTP请求。 I am pretty sure I know how to do this, i am just struggling with this small problem 我很确定我知道如何做到这一点,我只是在努力解决这个小问题

I didn't get the logger using the same string. 我没有使用相同的字符串获取记录器。 Once i did this, it worked: 一旦我做到这一点,它工作:

在此输入图像描述在此输入图像描述

Once i made the names the same, my program worked as the documentation specified. 一旦我的名字相同,我的程序就像指定的文档一样。

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

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