Search code examples
logbackslf4j

Split log to console and file with logback


I want to split up logging in a way that one special logger only logs to file, while all others log to console. I tried this:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="SIMULATOR_LOG_FILE" class="ch.qos.logback.core.FileAppender">
        <file>data_log.txt</file>
        <append>false</append>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level -%kvp- %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="Simulator" level="debug">
        <appender-ref ref="SIMULATOR_LOG_FILE"/>
    </logger>

    <root>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

but it still logs the Simulator logger to file and console, instead of just the file.

What am I doing wrong?


Solution

  • The solution is to add additivity="false" to the <logger> element