Logback loses log messages, even with discardingThreshold=0

Refresh

November 2018

Views

1.8k time

2

Reading this question about logback losing log statements, I added an discardingThreshold=0 statement. Sadly still some lines are missing in my log file. If I use only a RollingFileAppender, skipping AsyncAppender it works fine.

AsyncAppender misses every statement after Period 51, but the log file should contain entries till 96.

What do I do wrong?

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>

log file:

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 answers

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();
    }
}