Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j: Why is the root logger collecting all log types regardless the configuration?

I am having problem that even though I specify the level to ERROR in the root tag, the specified appender logs all levels (debug, info, warn) to the file regardless the settings. I am not a Log4j expert so any help is appreciated.

I have checked the classpath for log4j.properties (there is none) except the log4j.xml.

Here is the log4j.xml file:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>       <!-- ============================== -->     <!-- Append messages to the console -->     <!-- ============================== -->       <appender name="console" class="org.apache.log4j.ConsoleAppender">         <param name="Target" value="System.out" />          <layout class="org.apache.log4j.PatternLayout">             <!-- The default pattern: Date Priority [Category] Message\n -->             <param name="ConversionPattern" value="[AC - %5p] [%d{ISO8601}] [%t] [%c{1} - %L] %m%n" />         </layout>     </appender>      <appender name="logfile" class="org.apache.log4j.RollingFileAppender">         <param name="File" value="./logs/server.log" />         <param name="MaxFileSize" value="1000KB" />         <param name="MaxBackupIndex" value="2" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="[AC - %-5p] {%d{dd.MM.yyyy - HH.mm.ss}} %m%n" />         </layout>     </appender>      <appender name="payloadAppender" class="org.apache.log4j.RollingFileAppender">         <param name="File" value="./logs/payload.log" />         <param name="MaxFileSize" value="1000KB" />         <param name="MaxBackupIndex" value="10" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="[AC - %-5p] {%d{dd.MM.yyyy - HH.mm.ss}} %m%n" />         </layout>     </appender>      <appender name="errorLog" class="org.apache.log4j.RollingFileAppender">         <param name="File" value="./logs/error.log" />         <param name="MaxFileSize" value="1000KB" />         <param name="MaxBackupIndex" value="10" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="[AC - %-5p] {%d{dd.MM.yyyy - HH.mm.ss}} %m%n" />         </layout>     </appender>      <appender name="traceLog"         class="org.apache.log4j.RollingFileAppender">         <param name="File" value="./logs/trace.log" />         <param name="MaxFileSize" value="1000KB" />         <param name="MaxBackupIndex" value="20" />          <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern"                 value="[AccessControl - %-5p] {%t: %d{dd.MM.yyyy - HH.mm.ss,SSS}} %m%n" />         </layout>     </appender>      <appender name="traceSocketAppender" class="org.apache.log4j.net.SocketAppender">         <param name="remoteHost" value="localhost" />         <param name="port" value="4445" />         <param name="locationInfo" value="true" />     </appender>      <logger name="TraceLogger">         <level value="trace" /> <!-- Set level to trace to activate tracing -->         <appender-ref ref="traceLog" />          </logger>      <logger name="org.springframework.ws.server.endpoint.interceptor">         <level value="DEBUG" />         <appender-ref ref="payloadAppender" />     </logger>      <root>         <level value="error" />         <appender-ref ref="errorLog" />     </root>  </log4j:configuration> 

If I replace the root with another logger, then nothing gets logged at all to the specified appender.

<logger name="com.mydomain.logic">     <level value="error" />     <appender-ref ref="errorLog" /> </logger> 
like image 638
Petr Macek Avatar asked Sep 17 '08 19:09

Petr Macek


People also ask

What is root logger in log4j?

The rootlogger is always the logger configured in the log4j. properties file, so every child logger used in the application inherits the configuration of the rootlogger . The logging levels are (from smaller to greater) : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF .

What is root level in logging?

The root logger resides at the top of the logger hierarchy. It is exceptional in three ways: it always exists, its level cannot be set to null. it cannot be retrieved by name.

Which log level is the root logger configured by default?

By default, Root logger is configured to print out messages whose levels is ERROR. ERROR messages will not be logged by 'com' logger because it's level is FATAL.

How does log4j logging work?

Log4j allows logged messages to contain format strings that reference external information through the Java Naming and Directory Interface (JNDI). This allows information to be remotely retrieved across a variety of protocols, including the Lightweight Directory Access Protocol (LDAP).


2 Answers

The root logger resides at the top of the logger hierarchy. It is exceptional in three ways:

  • it always exists,
  • its level cannot be set to null
  • it cannot be retrieved by name.

The rootLogger is the father of all appenders. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy (including rootLogger)

For example, if the console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say L, then enabled logging requests for L and L's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.

From the log4j manual

To sum up:

If you want not to propagate a logging event to the parents loggers (say rootLogger) then add the additivity flag to false in those loggers. In your case:

<logger name="org.springframework.ws.server.endpoint.interceptor"         additivity="false">         <level value="DEBUG" />         <appender-ref ref="payloadAppender" /> </logger> 

In standard log4j config style (which I prefer to XML):

log4j.logger.org.springframework.ws.server.endpoint.interceptor = INFO, payloadAppender log4j.additivity.org.springframework.ws.server.endpoint.interceptor = false 

Hope this helps.

like image 77
monzonj Avatar answered Oct 14 '22 12:10

monzonj


Run your program with -Dlog4j.debug so that standard out gets info about how log4j is configured -- I suspected that it isn't configured the way that you think it is.

like image 45
James A. N. Stauffer Avatar answered Oct 14 '22 12:10

James A. N. Stauffer