Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback roll only on file size

Tags:

java

logback

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?

like image 436
Josh Avatar asked Feb 13 '15 22:02

Josh


2 Answers

Just to expand on sheltem's answer:

From my experiments, you have to use both a FixedWindowRollingPolicy and a SizedBasedTriggeringPolicy, as documented here. It appears that you can't use a SizedBasedTriggeringPolicy with a SizeAndTimeBasedRollingPolicy, for example: in my case logging then failed to happen.

Typical example in your xml file might be:

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">

    <file>../logs/MyProject/testrolling.html</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>../logs/MyProject/backup%i.htm
        </fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>10</maxIndex>
    </rollingPolicy>
    <triggeringPolicy
        class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>5MB</maxFileSize>
    </triggeringPolicy>

    <encoder class="...

Your backup files will then be called: backup1.htm, backup2.htm, etc.

It would appear that you cannot include a "%d" formatting directive in the fileNamePattern: I tried and logging failed to happen. However, according to the documentation you must include the "%i" formatting directive, which makes sense.

much later

This may be me being naive, but I'd have thought a logging app should be designed to produce output by default, i.e. always interpret directives so that something at least is produced, rather than nothing.

like image 145
mike rodent Avatar answered Sep 19 '22 03:09

mike rodent


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. ;)

like image 30
sheltem Avatar answered Sep 18 '22 03:09

sheltem