英文:
log4j child class not overriding root class configuration
问题
我以为子类级别(trace)应该覆盖根类级别(warn),但我只看到了warn及以上级别的日志。
英文:
Can someone give me pointers on below. I tought the child class level (trace) should override the root class level (warn), but I only see the warn and above logs.
I tought the child class level (trace) should override the root class level (warn). but I only see the warn and above logs.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="30">
<Appenders>
<Console name="StdOut" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %msg%n" />
</Console>
<File name="FileAppender" filename="LogOutput.log">
<PatternLayout
pattern="%d{dd MMM yyyy HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="FileAppender" />
</Root>
<!-- Uncomment the Logger definition below to have ONLY warn-level messages
and above be directed to the console -->
<Logger name="com.fdmgroup.logging.Demo" >
<AppenderRef ref="StdOut" level="trace"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" >
<AppenderRef ref="StdOut" level="trace"/>
<AppenderRef ref="FileAppender" level="trace"/>
</Logger>
</Loggers>
</Configuration>
答案1
得分: 1
你没有在记录器上设置TRACE
阈值,而是在附加器引用上设置了。因此,你的"com.fdmgroup.logging.Demo"记录器具有以下配置:
- 记录器上的
WARN
阈值(从根记录器继承), - "StdOut"附加器上的
TRACE
阈值, - 从根记录器继承的"FileAppender"附加器没有阈值(
additivity
的默认值为true
)。
只有未被记录器和附加器上的过滤器拒绝的消息才会被记录,因此只有WARN
消息会被记录。
你可能想要设置记录器的级别,而不是附加器引用的级别:
<Loggers>
<Root level="WARN">
<AppenderRef ref="FileAppender" />
</Root>
<Logger name="com.fdmgroup.logging.Demo" additivity="false" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
</Loggers>
英文:
You didn't set a TRACE
threshold on the logger, but on the appender reference. Hence your "com.fdmgroup.logging.Demo" logger has a:
WARN
threshold for the logger (inherited from the root logger),- a
TRACE
threshold for the "StdOut" appender, - no threshold for the "FileAppender" inherited from the root logger (the default value for
additivity
is `true).
A message is logged only if it not rejected by filter on the logger and the appender, hence only WARN
messages are logged.
You probably wanted to set the level for loggers, not appender refs:
<Loggers>
<Root level="WARN">
<AppenderRef ref="FileAppender" />
</Root>
<Logger name="com.fdmgroup.logging.Demo" additivity="false" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
<Logger name="com.fdmgroup.exercise.Runner" level="TRACE">
<AppenderRef ref="StdOut"/>
</Logger>
</Loggers>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论