gpt4 book ai didi

java - 配置 logback 以按大小滚动,但不按日期滚动

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:59 24 4
gpt4 key购买 nike

我正在努力配置 logback。这应该是微不足道的事情,但事实并非如此。我想按大小滚动日志,但不想按时间滚动日志。同时,我想保留滚动日志,其名称反射(reflect)滚动日期。我想保留日志的所有历史记录,因此固定窗口滚动策略在这里不起作用。

目前我正在使用

    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/my.log</file>
<append>true</append>
<encoder>
<pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} %-40.40thread %-5.5(Inv%X{context}) %-30.30(%method\(\)) %msg \(%class{0}.java:%line\)%n</pattern>
<immediateFlush>true</immediateFlush>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/mylog-%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>all</level>
</filter>
</appender>

尺寸和每月都会滚动。我想对 gz 文件使用指定的模式。

谢谢。

更新按照建议添加

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>

我将大小限制设置为 10MB 以查看收费情况现在我得到了

14:11:57,601 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING] - Appender [ROLLING] failed to append. java.lang.NullPointerException
at java.lang.NullPointerException
at at ch.qos.logback.core.rolling.helper.FileFilterUtil.afterLastSlash(FileFilterUtil.java:46)
at at ch.qos.logback.core.rolling.TimeBasedRollingPolicy.rollover(TimeBasedRollingPolicy.java:149)
at at ch.qos.logback.core.rolling.RollingFileAppender.attemptRollover(RollingFileAppender.java:158)
at at ch.qos.logback.core.rolling.RollingFileAppender.rollover(RollingFileAppender.java:137)
at at ch.qos.logback.core.rolling.RollingFileAppender.subAppend(RollingFileAppender.java:178)
at at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:103)
at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)
at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
at at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:273)
at at ch.qos.logback.classic.Logger.callAppenders(Logger.java:260)
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:442)
at at ch.qos.logback.classic.Logger.filterAndLog_1(Logger.java:414)
at at ch.qos.logback.classic.Logger.info(Logger.java:604)
at at my.util.LoggingTesting.main(LoggingTesting.java:21)

在我看来,ch.qos.logback.core.rolling.TimeBasedRollingPolicy 违反了单一责任原则。滚动策略假设配置日志的滚动方式,但在这种情况下,您也可以指定触发器(通过 timeBasedFileNamingAndTriggeringPolicy)

最佳答案

尝试ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy相反。

不要改变rolling policy只需使用 <triggeringPolicy>标签

关于java - 配置 logback 以按大小滚动,但不按日期滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31516256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com