Java – Logback roll only on file size

javalogback

I am using logback 1.0.13. I want my log file to roll solely based off of file size. If this takes 2 hours or 2 years, I don't care. I am having trouble figuring out how to do so. My appender is configured like so:

<appender name="serverFaultFile"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/folder/to/log/file.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>/folder/to/log/file-%d{yyyy-MM}.%i.log</fileNamePattern>
        <MaxHistory>2</MaxHistory>
        <cleanHistoryOnStart>true</cleanHistoryOnStart>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
            <MaxHistory>9</MaxHistory>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
        <pattern>[%p] [%d{ISO8601}] [%c] [%m]%n</pattern>
    </encoder>
</appender>

This solution rolls monthly, which isn't what I need. I tried completely dropping the -%d{dateformat} modifier, but then the file was never even created/logged to. I tried modifiers %G and %yyyy, but monthly was as fine grained as logback would apparently allow me to get (see this bug report). What am I missing?

Best Solution

You want a SizeBasedTriggeringPolicy: http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy, probably combined with a FixedWindowRollingPolicy.

Was a comment before, not sure it deserves it's own answer, but here it is. ;)

Related Question