log4j2中的层次结构无法正常工作
以下是类,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
log4j2中有两种继承:
添加性=“ false”
。在您的情况下,“ foo.bar”配置不使用“ foo”配置的附录。配置的另一个问题是,“ foo.bar”记录器的级别设置为
warn
,因此它永远不会发送任何与warn
不同的记录事件。和错误
。如果您不想限制记录器本身,但是appender,则可以在&lt; appenderref&gt;
上设置Level =“ Warn”
。如果您的目的是将不同的日志级别拆分为不同的日志文件,则可以使用
Level
设置(基本上是阈值过滤器)和或arouting> routinging> routingappender
):
There are two kinds of inheritance in Log4j2:
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 fromWARN
andERROR
. If you don't want to limit the logger itself, but the appender, you can setlevel="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) andLevelMatchFilter
s (or aRoutingAppender
):