Home > Back-end >  log4j child class not overriding root class configuration
log4j child class not overriding root class configuration

Time:01-09

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.

enter image description here 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>

CodePudding user response:

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>
  • Related