BinaryProbe
BinaryProbe

Reputation: 273

What is the option to "append" in Logback RollingFileAppender?

I have a simple question for using logback. I am using Logback for my application with RollingFileAppender. It works well but when I restart my application, It won't append existing file but gone somewhere.

Here is xml file for logback configuration for my application.

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- hourly rollover -->
        <fileNamePattern>/home/log/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <!-- or whenever the file size reaches 100MB -->
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <!-- keep 30 hours' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%-4r [%thread] %X{clientIP} %d{HH:mm:ss.SSS} [%p] [%thread@%C{1}:%L] - %m%n</pattern>
    </encoder>
</appender>

For example, when I first start application, It creates a log file in accordance with configuration above.

-rw-r--r-- 1 root root      6926 Nov  7 10:19 logFile.2015-11-07.log

But if I stop application and restart, I expect log to be appended on above file but It won't ( I even couldn't find where this missing log exist. )

Thanks in advance.

Upvotes: 2

Views: 6164

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280132

The documentation states

Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern.

By computed anew, they mean that a new file will be created (or an existing one truncated).

So just provide an appropriately named file property.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/home/log/logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- hourly rollover -->
        <fileNamePattern>/home/log/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <!-- or whenever the file size reaches 100MB -->
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <!-- keep 30 hours' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%-4r [%thread] %X{clientIP} %d{HH:mm:ss.SSS} [%p] [%thread@%C{1}:%L] - %m%n</pattern>
    </encoder>
</appender>

With file, you're telling Logback which is the active file and therefore which to append to (after determining if a rollover is necessary).

Upvotes: 2

Related Questions