2
votes

We are using Spring Boot and are including org/springframework/boot/logging/logback/base.xml in our logback-spring.xml file. I need to override root logger to exclude/disable/override appenders declared in included file, in particular a CONSOLE appender (well, and remove FILE appender).

I've tried declaring a root logger in logback-spring.xml with my own CONSOLE appender, but that only duplicates the output. I've tried declaring an empty root logger (to set a different logging level) and a new appender with the same name as included one ("CONSOLE"), but that is misleading (as root logger is empty) and still does not remove FILE appender.

Misleading version of logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>

<configuration scan="true">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <!-- bunch of loggers -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
    </appender>
    <root level="ERROR">
    </root>
</configuration>

What I would expect is something like this:

    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <appender name="CUSTOM-CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
    </appender>
    <root level="ERROR">
        <appender-ref ref="CUSTOM-CONSOLE"/>
    </root>

And then included CONSOLE and FILE appenders are not used.

Or maybe re-declaring some dummy/no-op CONSOLE and FILE appenders to override included configuration.

EDIT: My question is about appenders inherited from included file hence suggestions in Can I disable an appender in logback? don't really address the issue.

1
I've updated my question to address that possible duplicate. - Aivaras

1 Answers

0
votes

There seems to be no way to remove/override an appender. However according to this http://logback.qos.ch/codes.html#earlier_fa_collision

If a FileAppender/RollingFileAppender defined earlier has the same File option as the current appender, then those two appenders are in collision as FileAppender instances cannot share the same output target. To prevent loss of data, the current appender will not start. Make sure that each appender has a unique File option.

it is possible to disable an appender. However this way is more of a hack than a proper solution.