Search code examples
springtomcatlogginglog4jlog4j2

Log4j- difference between Configuration and Level


Can someone explain what the following configuration of Log4j does?

I'm particulary confused with the tag Configuration = WARN and Root level="INFO".

If Configuration means capture the WARN messages, why is Root level INFO? how do they relate?

Does the snippet assign to the Root logger the appender Console?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>


Solution

  • Piotr's answer is correct. To give it a bit more detail though, Log4j actually has multiple implementations of the Log4j API.

    Within the API is SimpleLogger which allows minimal logging when no logging implementation is present. It only allows minimal configuration via system properties.

    Also within the API is StatusLogger. It also implements the Log4j API. The Log4j implementation uses the Log4j API internally for all its diagnostic logging. Since it has to log startup and configuration of itself it uses the StatusLogger to do that. This implementation is somewhat similar to SimpleLogger in that it is not very configurable. Pretty much the only thing you can control is whether it logs to the console or a file.

    If you set status="OFF" then no diagnostic information will ever be printed by Log4j even if your configuration is messed up. However, even then you can specify -Dlog4j2.debug=true on startup and Log4j will emit debug logs. These are very helpful when things go wrong in configuration.