Search code examples
logback

Logback configuration without explicit appender defining


The following Logback configuration, one of the loggers explicitly defines it's appender, another one not:

<configuration scan="true" scanPeriod="60 seconds">

<appender name="AMAZING_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>XXX</pattern>
    </encoder>
</appender>

<logger name="org.springframework">
    <level value="ERROR" />
</logger>

<logger name="com.company" additivity="false">
    <level value="INFO" />
    <appender-ref ref="AMAZING_APPENDER"/>
</logger>

</configuration>

What appender would the logger without the explicit appender specification would use, if any at all?


Solution

  • By "logger without the explicit specification" I think you are referring to this: <logger name="org.springframework">.

    If so, then there is no appender available for that logger and any log events emitted by classes in the org.springframework (regardless of the log level) will be ignored.

    If you tweak your logback.xml slightly to add this: <configuration debug="true"> you'll see a WARN event of this nature ...

    No appenders present in context [default] for logger [...]

    ... emitted when a class in the org.springframework emits a log event at ERROR level.

    If you want events from the org.springframework namespace (or more generally, from any namespace other than com.company) to be logged then you should add a <root> configuration. For example:

    <root level="INFO">
        <appender-ref ref="AMAZING_APPENDER"/>
    </root>
    

    If you add a root and point it at AMAZING_APPENDER then you should probably set additive="false" on logger name="com.company" otherwise anything logged from the com.company namespace will be logged twice. So, perhaps your desired configuration will be:

    <root level="INFO">
        <appender-ref ref="STANDARD_APPENDER"/>
    </root>