java Logback 仅根据文件大小回滚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28509787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:42:45  来源:igfitidea点击:

Logback roll only on file size

javalogback

提问by Josh

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:

我正在使用 logback 1.0.13。我希望我的日志文件仅根据文件大小滚动。如果这需要 2 小时或 2 年,我不在乎。我很难弄清楚如何做到这一点。我的 appender 配置如下:

<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?

这个解决方案每月滚动一次,这不是我需要的。我尝试完全删除 -%d{dateformat} 修饰符,但是该文件甚至从未被创建/记录到。我尝试了修饰符 %G 和 %yyyy,但每月的粒度与 logback 显然允许我获得一样细粒度(请参阅此错误报告)。我错过了什么?

回答by sheltem

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

您需要 SizeBasedTriggeringPolicy:http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy ,可能与 FixedWindowRollingPolicy 结合使用。

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

之前是评论,不确定它是否值得自己回答,但现在是。;)

回答by mike rodent

Just to expand on sheltem's answer:

只是为了扩展 Sheltem 的答案:

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

根据我的实验,您必须同时使用 aFixedWindowRollingPolicy和 a SizedBasedTriggeringPolicy,如此处所述。似乎您不能将 aSizedBasedTriggeringPolicy与a一起使用SizeAndTimeBasedRollingPolicy,例如:在我的情况下,日志记录失败。

Typical example in your xml file might be:

您的 xml 文件中的典型示例可能是:

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

您的备份文件将被称为:backup1.htm、backup2.htm 等。

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.

看起来您不能在以下内容中包含“%d”格式指令fileNamePattern:我尝试过,但日志记录失败。但是,根据文档,您必须包含“%i”格式指令,这是有道理的。

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.

这可能是我太天真了,但我原以为日志应用程序应该设计为默认生成输出,即始终解释指令,以便至少生成一些东西,而不是什么都不生成。