maxHistory 参数的实现方式似乎存在问题。我在时间间隔内滚动多次(这里每分钟),创建以下文件:(maxHistory = 3)
- trace.log(活动文件)
- 跟踪-2013-03-13-16.14.0.log.zip
- 跟踪-2013-03-13-16.13.0.log.zip
- 跟踪-2013-03-13-16.12.2.log.zip
- trace-2013-03-13-16.12.1.log.zip
- trace-2013-03-13-16.12.0.log.zip
下一次翻转发生,16.13.0.log.zip 文件被删除,而不是最旧的文件(12.{0,1,2},正如我所预料的那样。这是预期的行为吗?
public static Logger createTimeAndSizeRollingLogger(String fileBase, String maxSize, int maxHistory, String resolutionPattern) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
appender.setContext(loggerContext);
appender.setFile(fileBase + ".log");
TimeBasedRollingPolicy<ILoggingEvent> timePolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
timePolicy.setFileNamePattern(fileBase + "_%d{" + resolutionPattern + "}-%i.log.zip");
timePolicy.setContext(loggerContext);
timePolicy.setMaxHistory(maxHistory);
timePolicy.setParent(appender);
appender.setRollingPolicy(timePolicy);
SizeAndTimeBasedFNATP<ILoggingEvent> rollingPolicy = new SizeAndTimeBasedFNATP<ILoggingEvent>();
rollingPolicy.setMaxFileSize(maxSize);
rollingPolicy.setTimeBasedRollingPolicy(timePolicy);
rollingPolicy.setContext(loggerContext);
timePolicy.setTimeBasedFileNamingAndTriggeringPolicy(rollingPolicy);
timePolicy.start();
rollingPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%msg%n");
encoder.start();
appender.setEncoder(encoder);
appender.start();
Logger logger = loggerContext.getLogger(fileBase);
logger.setLevel(Level.TRACE);
logger.addAppender(appender);
StatusPrinter.print(loggerContext);
return logger;
}