1

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;
}
4

2 回答 2

0

这似乎在最新的 1.1.0 SNAPSHOT http://jira.qos.ch/browse/LOGBACK-162中得到修复

于 2013-03-14T07:57:42.037 回答
0

这是一个已知的问题。MaxHistory 不适用于 SizeAndTimeBasedFNATP。你可以在这里查看

你会发现很多人说它解决了,但事实并非如此。如果你想修复它,你可以编辑位于 logback-core 中的 SizeAndTimeBasedFNATP.java。你可以在这里看到它。在我看来,isTriggeringEvent 里面是添加代码的地方。

于 2015-11-20T14:54:41.127 回答