即使discardingThreshold = 0,Logback也会丢失日志消息

时间:2014-05-13 09:57:54

标签: java logging logback

关于logback丢失日志语句读取this question,我添加了discardingThreshold = 0语句。遗憾的是,我的日志文件中仍然缺少一些行。如果我只使用RollingFileAppender,跳过AsyncAppender它可以正常工作。

AsyncAppender在第51期之后错过了每个语句,但是日志文件应包含直到96的条目。

我做错了什么?

logback.xml

<configuration>
<!-- Einstellungen fuer die Konsolenausgabe -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%-5p %c{2} - %m%n</pattern>
    </encoder>
</appender>

<!-- Einstellungen fuer die Logdatei -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>log/simulation.log</file>
    <append>true</append>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>simulation.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>1024KB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
        <pattern>%d{ISO8601} %-5p %c - %m%n</pattern>
    </encoder>
</appender>

<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <discardingThreshold>0</discardingThreshold>
    <appender-ref ref="FILE" />
</appender>

<root level="trace">
    <appender-ref ref="ASYNC" />
    <appender-ref ref="STDOUT" />
</root>
</configuration>

日志文件:

INFO  d.t.w.o.z.s.SimulationHospital - Simulation initialize
TRACE d.t.w.o.z.s.SimulationHospital - Period 0 starts
TRACE d.t.w.o.z.s.SimulationHospital - Arrival occurs
TRACE d.t.w.o.z.s.SimulationHospital - Period 1 starts
TRACE d.t.w.o.z.s.SimulationHospital - Arrival occurs
TRACE d.t.w.o.z.s.SimulationHospital - Finish treatment
TRACE d.t.w.o.z.s.SimulationHospital - Period 2 starts
TRACE d.t.w.o.z.s.SimulationHospital - Arrival occurs
TRACE d.t.w.o.z.s.SimulationHospital - Finish treatment
...
...
...
TRACE d.t.w.o.z.s.SimulationHospital - Arrival occurs
TRACE d.t.w.o.z.s.SimulationHospital - Finish treatment
TRACE d.t.w.o.z.s.SimulationHospital - Period 96 starts
TRACE d.t.w.o.z.s.SimulationHospital - Finish treatment
INFO  d.t.w.o.z.s.SimulationHospital - Simulation is running
REPORT on Tally stat. collector ==> Average wait time per day
    num. obs.      min          max        average     standard dev.
       143        0.000        0.000        0.000        0.000
  90.0% confidence interval for mean (student): (     0.000,     0.000 )

INFO  d.t.w.o.z.s.SimulationHospital - Simulation finished

1 个答案:

答案 0 :(得分:3)

您需要正确关闭上下文,例如流中的刷新:

@Component
public class LogsFlusher {

    private static final Logger LOG = LoggerFactory.getLogger(LogsFlusher.class);

    @PreDestroy
    public void flushLogs() {
        LOG.info("Shutdown logger context.");
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.stop();
    }
}