log4j子类未覆盖根类配置

huangapple go评论81阅读模式
英文:

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.

log4j子类未覆盖根类配置
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:

    &lt;Loggers&gt;   
        &lt;Root level=&quot;WARN&quot;&gt;
            &lt;AppenderRef ref=&quot;FileAppender&quot; /&gt;
        &lt;/Root&gt;
        &lt;Logger name=&quot;com.fdmgroup.logging.Demo&quot; additivity=&quot;false&quot; level=&quot;TRACE&quot;&gt;
            &lt;AppenderRef ref=&quot;StdOut&quot;/&gt;
        &lt;/Logger&gt;     
        &lt;Logger name=&quot;com.fdmgroup.exercise.Runner&quot; level=&quot;TRACE&quot;&gt;
            &lt;AppenderRef ref=&quot;StdOut&quot;/&gt;
        &lt;/Logger&gt;
    &lt;/Loggers&gt;

huangapple
  • 本文由 发表于 2023年1月9日 01:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定