由于我没有找到答案,因此,如果被视为其他问题的重复,我们深表歉意。
我使用SiftingAppender为每个服务(3个服务)和TimeBasedRollingPolicy创建不同的日志文件,因此日志文件的滚动每天都会发生。
LogDiscriminator.java生成用于SiftingAppender策略的log_discriminator进行回滚。这里有两种情况:
请问,是否可以仅根据日期删除早于MaxHistory的日志?我的意思是无论名称(案例2)如何,logback都可以删除日志文件。还有另一种方法可以做到这一点吗?
登录
<?xml version="1.0"?>
<configuration>
<!-- ... -->
<appender name="MAIN" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>log_discriminator</key>
<defaultValue>LOG_FILE_KEY_UNKNOWN</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${log_discriminator}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.folder}/${log_discriminator}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily roll-over -->
<FileNamePattern>${log.folder}/${log_discriminator}.%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>10</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<prudent>true</prudent>
</appender>
</sift>
</appender>
<!-- ... -->
</configuration>
LogDiscriminator.java
public class LogDiscriminator {
...
public static void setLogFileType(String appType) {
// Case 1 - logback was able to delete after 10 days of log
// MDC.put("log_discriminator", appType); // e.g. ser_1, ser_2 or ser_3 (3 services)
// Case 2 - added UUID (just to distingush between instances of the same app - e.g. 2 instances of ser_1), logback is not able to delete
MDC.put("log_discriminator", String.format("%s_%s", appType, UUID.randomUUID().toString()));
}
...
}