Logback roll only on file size

2019-06-23 22:52发布

问题:

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?

回答1:

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



回答2:

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.



标签: java logback