ulquiorra
ulquiorra

Reputation: 945

logback loading all appenders

I have a logback.xml file with 3 appender in : 1 ConsoleAppender and 2 FileAppender

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

<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}] %msg%m%n%ex</pattern>
        </encoder>
    </appender>

    <appender name="bAppender" class="ch.qos.logback.core.FileAppender">
        <file>logs/b.txt</file>
        <append>true</append>
        <encoder>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}] -[%t] %-4r %-5p %c : %n%m%n</pattern>
        </encoder>
    </appender>


    <appender name="aAppender" class="ch.qos.logback.core.FileAppender">
        <file>logs/a.txt</file>
        <append>false</append>
        <encoder>
            <pattern>[%d{dd/MM/yyyy : HH:mm:ss}  %level - %logger] %m%n</pattern>
        </encoder>
    </appender>

    <logger name="aLogger" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="aAppender" />        
    </logger>

    <root level="info">
        <appender-ref ref="stdout"/>
    </root>
</configuration>

The problem is when i launch the program associated with aLogger(Logger LOGGER = LoggerFactory.getLogger("aLogger");. It add the bAppend as well ( and so create a empty b.txt file ....)

It's like logback call and execute all appender when a logger is called regardless attached appender. How to avoid this behaviour ?

Thank you very much

Upvotes: 2

Views: 1387

Answers (2)

S_intg
S_intg

Reputation: 332

Maybe late to answer, but I do need same result as you @ulquiorra So based on my search I did come across this solution: use SiftingAppender example :

<appender name="bAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator>
        <key>logFileName</key>
        <defaultValue>b</defaultValue>
    </discriminator>
    <sift>
        <appender name="${logFileName}" class="ch.qos.logback.core.FileAppender">
            <file>logs/${logFileName}.txt</file>

            <encoder>
                <pattern>[%d{dd/MM/yyyy : HH:mm:ss}  %level - %logger] %m%n</pattern>
            </encoder>
        </appender>
    </sift>
</appender>

From LogBack Jira: https://jira.qos.ch/browse/LOGBACK-202

If you wrap a FileAppender with a SiftingAppender, then the FileAppender will be created on the first log event, and thus the log file will be created in a lazy manner.

Take a look also on documentation https://logback.qos.ch/manual/appenders.html#SiftingAppender

Another example on https://www.mkyong.com/logging/logback-xml-example/

Upvotes: 3

sherb
sherb

Reputation: 6105

In short, I think the answer is 'no', there isn't a way to prevent b.txt from being created. Looking at the source for FileAppender, it appears that the output file (and the corresponding parent folder hierarchy) is created when the appender is started (which happens when logback is initialized). This design is deliberate, since this approach will save considerable time during logging.

Upvotes: 0

Related Questions