http://logback.qos.ch/manual/appenders.html#RollingFileAppender
1.基于时间的滚动策略
TimeBasedRollingPolicy
就好比一个小车,推着走。车身所压的时间就是要保留日志的时间。
<configuration> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>${catalina.base}/logs/${CONTEXT_NAME}/%d{yyyy-MM-dd}/logFile.%d{yyyy-MM-dd}.log </fileNamePattern> <!-- keep 30 days‘ worth of history --> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n </pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
2.基于大小和时间的文件命名和触发策略
http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
FNATP means File Naming And Triggering Policy
达到最大值便压缩;可根据每天时间变化生成文件夹。
可以设置两个rolling :INFO 和 ERROR .
<configuration> <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${catalina.base}/logs/${CONTEXT_NAME}/%d{yyyy-MM-dd}/mylog-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>1mb</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="ROLLING" /> </root> </configuration>
由以下代码可知,不加单位为byte, 单位只能加kb、mb、gb ,不区分大小写。
if (unitStr.equalsIgnoreCase("")) { coefficient = 1; } else if (unitStr.equalsIgnoreCase("kb")) { coefficient = KB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("mb")) { coefficient = MB_COEFFICIENT; } else if (unitStr.equalsIgnoreCase("gb")) { coefficient = GB_COEFFICIENT; } else { throw new IllegalStateException("Unexpected " + unitStr); } return new FileSize(lenValue * coefficient);
注:1、 2 策略不能混用,因为不能同时生效
时间: 2024-10-11 00:45:49