现在需要将logback的最大日志数量提高到40个,由于logback内部最大只支持20个,设置超过20后也只输出20个文件。
重写FixedWindowRollingPolicy类如下:
package com.logback.test;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
public class CustomFixedWindowRollingPolicy extends FixedWindowRollingPolicy {
private static int MAX_WINDOW_SIZE = 500;
@Override
protected int getMaxWindowSize() {
return MAX_WINDOW_SIZE;
}
}
logback.xml配置如下:
<appender name="realMasterAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.home}/real.log</File>
<rollingPolicy class="com.logback.test.CustomFixedWindowRollingPolicy">
<fileNamePattern>${log.home}/real.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>100</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>40MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date %level [%file : %line] %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
运行后后台报错如下:
17:32:44,585 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:32:44,632 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
17:32:44,632 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [infoAppender]
17:32:44,632 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [rollingPolicy] of type [com.logback.test.CustomFixedWindowRollingPolicy] java.lang.ClassNotFoundException: com.logback.test.CustomFixedWindowRollingPolicy
at java.lang.ClassNotFoundException: com.logback.test.CustomFixedWindowRollingPolicy
at at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:494)
at at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
at at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:398)
at at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:105)
at at java.lang.ClassLoader.loadClass(Unknown Source)
at at ch.qos.logback.core.util.Loader.loadClass(Loader.java:124)
at at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:100)
at at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:275)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:147)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:129)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50)
求解啊!