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

Viewed 73571

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>
6 Answers

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.

To add on to what James A. N. Stauffer and cynicalman said - I would bet that there is another log4j.xml / log4j.properties on your classpath other than the one you wish to be used that is causing log4j to configure itself the way it is.

-Dlog4j.debug is an absolute killer way to troubleshoot any log4j issues.

Two things: Check additivity and decide whether you want log events captured by more detailed levels of logging to propagate to the root logger.

Secondly, check the level for the root logger. In addition you can also add filtering on the appender itself, but this should normally not be necessary.

If you are using a log4j.properties file, this file is typically expected to be in the root of your classpath, so make sure it's there.

Related