javaloggingslf4jlogback

use of <logger> element is logback.xml


I created a simple web application with slf4j-logback logging. I used the below configuration that prints log statements to mylog.log file.

<configuration>
    <appender name="fileAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/mylog.log</file>    
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>
    
    <root>
        <level value="DEBUG" />
        <appender-ref ref="fileAppender" />
    </root>
</configuration>

The above worked fine.

I came across one logger element as

<logger name="mylog" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="fileAppender" />
</logger>

What is the use of this logger element? Will it make any difference as my first configuration worked fine?


Solution

  • The <logger> is not needed because you are using <root> logger. The root configuration is inherited by children logger configurations and can be overriden. See Logback configuration documentation.