我很难配置logback。这应该是微不足道的,但事实并非如此。 我想按日期滚动日志但不准时。同时我想保持滚动日志的名称反映滚动日期。 我想保留日志的所有历史记录,因此FixedWindowRollingPolicy在这里不起作用。
目前我正在使用
<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档案使用指定的模式。
感谢。
UPDATE 按建议添加
<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)
答案 0 :(得分:0)
请尝试ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy
。
不要更改rolling policy
只需使用<triggeringPolicy>
代码