Android logback SizeBasedTriggeringPolicy不会触发

时间:2016-03-23 06:45:25

标签: android logging logback rollingfileappender

我使用logback for android,我在代码中进行配置,因为我希望日志目录根据手机动态变化(例如某些手机有外部存储空间,我想写入,但其他手机没有,所以我们必须重定向到内部存储。)

这是我的配置代码:

File logFilesDir = getLogFilesDir(this);
LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
lc.reset();

RollingFileAppender<ILoggingEvent> fileAppender = null;
lc.putProperty("LOG_DIR", logFilesDir.getAbsolutePath());

PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(lc);
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.start();

File logFile = new File(logFilesDir, "MyApplication.log");

FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(lc);
rollingPolicy.setFileNamePattern("${LOG_DIR}/MyApplication.%i.log");
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(5);

SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setContext(lc);
triggeringPolicy.setMaxFileSize("1KB"); // for testing; would otherwise be 5MB

fileAppender = new RollingFileAppender<>();
fileAppender.setContext(lc);
fileAppender.setFile(logFile.getAbsolutePath());
fileAppender.setEncoder(encoder);
fileAppender.setRollingPolicy(rollingPolicy);
fileAppender.setTriggeringPolicy(triggeringPolicy);

rollingPolicy.setParent(fileAppender);

fileAppender.start();
rollingPolicy.start();
triggeringPolicy.start();

// add the newly created appenders to the root logger;
// qualify Logger to disambiguate from org.slf4j.Logger
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.addAppender(fileAppender);

root.info("----- Application onCreate -----");
StatusPrinter.print(lc);

日志写入正确的文件夹并且它都正常工作,除非我的日志文件超过最大文件大小(在我的测试用例中为1KB),它无法翻转。相反,该文件不断增长。

StatusPrinter.print写下以下输出,看起来一切正常

|-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [assets/logback.xml]
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[null] - Active log file name: /storage/emulated/0/MyApplication/logs/MyApplication.log
|-INFO in ch.qos.logback.core.rolling.RollingFileAppender[null] - File property is set to [/storage/emulated/0/MyApplication/logs/MyApplication.log]
|-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@34b71b53 - No compression will be used

我已经查看了许多其他类似的问题,通常人们似乎错过了RollingFileAppender或者没有WRITE_EXTERNAL_STORAGE权限......在我看来并不是这样就像我碰到任何一个案子一样。关于我的代码(与人们通常使用的XML相比)是不同的?

我有什么办法可以解决这个问题或进一步排除故障吗?

由于

1 个答案:

答案 0 :(得分:0)

我无法立即告诉问题是什么,但添加OnConsoleStatusListener以查看翻转过程中会发生什么会很有帮助:

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
lc.reset();
OnConsoleStatusListener.addNewInstanceToContext(lc);

来自RollingFileAppender的翻转问题通常是由其配置中的绝对路径和相对路径混合造成的(Issue #117Issue #110)或缺少WRITE_EXTERNAL_STORAGE权限(这是不是你的情况)。来自OnConsoleStatusListener的调试输出可能会有所启发。如果没有,我可以尝试调试它,如果您创建GitHub Issue,最好使用Android Studio项目来重现问题。