登录我的应用程序可以正常使用log4j库1.1.4和slf4j-api 1.7.25。
但是在将log4j库从版本1.1.4更新到1.1.5(或更高版本)之后,日志文件只是停止创建,没有异常或产生警告。即使版本1.1.5相当陈旧,所以我宁愿使用更新版本。在配置如何工作方面肯定有所改变,但我找不到什么。
这个简单的代码将在log4j 1.1.4中创建滚动日志文件,但不生成具有更新版本的日志文件。缺少什么?
Maven依赖项是:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
代码:
package com.scratch.mylogtest; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.encoder.PatternLayoutEncoder; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.rolling.*; import static java.nio.charset.StandardCharsets.UTF_8; /** * A test of log file configuration and roll-over. It should simply create a log file then write enough data to it to cause the * file to roll-over to a new log file. */ public class LogbackTest1 { public static void main(String[] args) { final String LOG_LAYOUT = "%-5level \\(%logger{0}\\) [%date]: %m%n"; final org.slf4j.Logger rootLogger = org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); rootLogger.info("Test of root logger prior to initialization"); ((ch.qos.logback.classic.Logger) rootLogger).setLevel(ch.qos.logback.classic.Level.DEBUG); //set up the logging SLF4JBridgeHandler.removeHandlersForRootLogger(); //remove normal JUL logging... SLF4JBridgeHandler.install(); //...and route JUL logging to SLF4J LoggerContext loggerContext = ((ch.qos.logback.classic.Logger) rootLogger).getLoggerContext(); //appender RollingFileAppender rollingFileAppender = new RollingFileAppender(); rollingFileAppender.setContext(loggerContext); rollingFileAppender.setFile("C:\\ProgramData\\LogTest\\log-test.log"); //encoder PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder(); patternLayoutEncoder.setContext(loggerContext); patternLayoutEncoder.setCharset(UTF_8); patternLayoutEncoder.setPattern(LOG_LAYOUT); patternLayoutEncoder.start(); //rolling policy FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy(); fixedWindowRollingPolicy.setContext(loggerContext); fixedWindowRollingPolicy.setParent(rollingFileAppender); fixedWindowRollingPolicy.setFileNamePattern("C:\\ProgramData\\LogTest\\log-test.log.%i"); fixedWindowRollingPolicy.setMaxIndex(3); fixedWindowRollingPolicy.start(); //triggering policy SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy(); triggeringPolicy.setMaxFileSize("100KB"); //start appender rollingFileAppender.setEncoder(patternLayoutEncoder); rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy); rollingFileAppender.setTriggeringPolicy(triggeringPolicy); rollingFileAppender.start(); //add appender ((ch.qos.logback.classic.Logger) rootLogger).addAppender(rollingFileAppender); final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LogbackTest1.class); // Write some data into the logs. Write enough data that it should cause the log file to roll-over to a new file logger.info("Starting"); int logTestLines = 5000; for (int i = logTestLines; i > 0; i--) { logger.info("Log test line: " + i); } logger.info("Exiting"); } }
答案 0 :(得分:0)
找到解决方案!
从logback版本1.1.5开始,需要调用SizeBasedTriggeringPolicy.start()
。在版本1.1.3中没有必要。
从版本1.1.9开始,设置最大文件大小的方法也会发生变化,因此代码应如下所示:
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("100KB"));
triggeringPolicy.start();
我通过使用调试方法找到了问题
StatusPrinter.print(loggerContext);
打印此信息:
15:47:22,788 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[null] - TriggeringPolicy has not started. RollingFileAppender will not start
但如果您不使用StatusPrinter
,则不会打印任何警告。