Search code examples
logbacklogback-classicsifting-appender

RollingFileAppender not working when used inside SiftingAppender


I have the following logback setup

<appender name="TEST-SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator class="..."/>
    <sift>
        <appender name="ROLL-${fileName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../log/${fileName}.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>../log/${fileName}%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>10</maxIndex>
            </rollingPolicy>

            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>5MB</maxFileSize>
            </triggeringPolicy>

            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    </sift>
  </appender>

The discriminator class returns a value by parsing the loggerName. The key is defined as "fileName".

The logs rollover fine when I test just the RollingFileAppender (after replacing the variable references of ${fileName} with a static value) but when I have it nested under SiftingAppender, the logs do not roll over. I tested the sifting appender with "FileAppender" and it is able to create the right file name based on the discriminator.

I also tested the same configuration by using the discriminator as

<discriminator>
   <key>fileName</key>
   <defaultValue>appname</defaultValue>
</discriminator>

and removing the class tag. This creates appname.log but does not roll over.

Setting debug="true" did not write any additional information to the log file.

Am I missing something here? How do I implement RollingFileAppender inside a SiftingAppender?


Solution

  • I figured out the issue with my setup. My logback.xml has to two RollingFileAppenders (one nested in the sifter and one outside). Appender A was writing to application.log and Appender B in some circumstances was writing to application.log as well (i.e. ${fileName} evaluated to application). So if I remove appender A or rename Appender A's fileName, the logs roll over as configured. This probably means Appender A or B could not close and rename the file because the other appender still has a lock on it?

    To test this I used an AsyncAppender as follows:

    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
       <appender-ref ref="FILE" />
    </appender>
    

    where "FILE" is the name of Appender A. Using this configuration works but I see some strange behavior where the files do not rollover at the exact size specified and in some cases, the files are renamed with index 10 and get deleted automatically. Since this behavior is not very reliable, for now I got rid of Appender A.