上次发现了logback1.11的一个bug,即有线程持续写入log,则log文件不会按设定模式进行轮换。
但发现同样采用logback1.11的另外一个工程,它的日志文件就没有错误,于是参照其配置文件将logback.xml设定如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!-- Where are log files --> <property name="LOG_HOME" value="d:/logs" /> <!-- Output to Console --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <!-- Output to File --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/logbackCfg3new.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>10KB</maxFileSize> <MaxHistory>60</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <!-- log level TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,default:DEBUG。--> <root level="ALL"> <appender-ref ref="STDOUT" /> <!-- show log on console --> <appender-ref ref="FILE" /> <!-- show log in file --> </root> </configuration>
上面蓝字部分整段替换掉了原有的appender,而红色部分是关键所在。
然后,依然启动三个线程写log:
线程一:
package logbackCfg; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { private final static Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) throws InterruptedException { while(true) { Thread.sleep(5000); logger.info("秦时明月汉时关"); logger.error("万里长征人未还"); logger.debug("但使龙城飞将在"); logger.trace("不教胡马度阴山"); } } }
线程二:
package logbackCfg; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App2 { private final static Logger logger = LoggerFactory.getLogger(App2.class); public static void main(String[] args) throws InterruptedException { while(true) { Thread.sleep(8000); logger.info("九里山前作战场"); logger.error("牧童拾得旧刀枪"); logger.debug("微风吹皱乌江水"); logger.trace("恰似虞姬别霸王"); } } }
线程3:
package logbackCfg; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NewDayThread { private final static Logger logger = LoggerFactory.getLogger(NewDayThread.class); public static void main(String[] args) throws InterruptedException { while(true) { Date currentTime = new Date(); if(currentTime.compareTo(fixedDate())>0) { Thread.sleep(8000); logger.info("万里赴戎机,关山度若飞。"); logger.error("朔气传金柝,寒光照铁衣。"); logger.debug("将军百战死,壮士十年归。"); } } } private static Date fixedDate() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return formatter.parse("2020-01-17 13:10:00"); } catch (ParseException e) { return new Date(); } } }
然后到目录中查看,发现没有发生因线程持续写log而导致文件不发生轮换的事情:
这样,1.11版本的bug就被绕过去了,只是修改logback配置文件的事情,可以说以最小代价达成了目的。
代码下载:https://files.cnblogs.com/files/xiandedanteng/logbackCfg3_20200117_2.zip
--END-- 2020-01-17 14:48
2020-01-21补记,下面的xml工作得正常:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!-- Where are log files --> <property name="LOG_HOME" value="d:/logs" /> <!-- Output to Console --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <!-- Output to File --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/logbackCfg3new.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>1MB</maxFileSize> <MaxHistory>60</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <!-- log level TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,default:DEBUG。--> <root level="ALL"> <appender-ref ref="STDOUT" /> <!-- show log on console --> <appender-ref ref="FILE" /> <!-- show log in file --> </root> </configuration>