我正在开发一个spring-boot项目,我正在使用logback和slf4j进行日志记录。应用程序部署在外部tomcat上,我使用的是logback 1.1.6。 我用标记创建了一个appender,它工作正常但旋转策略不起作用。
这是logback.xml的条目
<appender name="FAILED-QUERY"
class="com.xyz.config.HourlyFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>FAILEDQUERY</marker>
</evaluator>
<onMismatch>DENY</onMismatch>
<onMatch>NEUTRAL</onMatch>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/xyz_otp_logs_failed_query.%d{yyyy-MM-dd-HH-mm}.log
</fileNamePattern>
<!-- <timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
</timeBasedFileNamingAndTriggeringPolicy> -->
</rollingPolicy>
</appender>
<logger name="com.xyz" level="info"
additivity="false">
<appender-ref ref="FAILED-QUERY" />
<!-- <appender-ref ref="STDOUT" /> -->
</logger>
旋转政策的代码在
之下public class HourlyFileAppender<E> extends RollingFileAppender<E> {
private static long start = System.currentTimeMillis(); // minutes
private int rollOverTimeInSecond = 60*60;
@Override
public void rollover()
{
long currentTime = System.currentTimeMillis();
int maxIntervalSinceLastLoggingInMillis = rollOverTimeInSecond * 1000;
if ((currentTime - start) >= maxIntervalSinceLastLoggingInMillis)
{
super.rollover();
start = System.currentTimeMillis();
}
}
}
虽然其他appender的相同设置工作正常。