[logback-user] how to graceful shutdown logback with AsyncAppenders and without immediateFlush

Gena Makhomed gmm at csdoc.com
Tue Mar 18 11:37:18 CET 2014


Re: AsyncAppenderBase not flushing queue during JVM shutdown

Michael Reinhold,
I can propose better solution of described problem:

we just need make two modes of work for logback:

1. normal mode, as it work all time after configuration finishing.

2. ready to shutdown mode, - fail safe mode, without messages lost.

switching from "normal mode" to "ready to shutdown mode"
do via JVM shutdown hook, - just one shutdown hook for logback.

if logback turned from "normal mode" to "ready to shutdown mode",
logback change own behavior in such way:

1. all appenders turn "immediateFlush = true",
regardless of initial value in logback configuration,
to prevent message lost during forthcoming JVM shutdown.

2. all AsyncAppenders turns into transparent and sync mode.
if AsyncAppender work in this "transparent and sync mode",
all behavior of

app -> AsyncAppender -> RollingFileAppender -> file

completly identical to behaviour of

app -> RollingFileAppender -> file

after switching from "normal mode" to "ready to shutdown mode",
JVM shutdown hook finished work and now logback is ready
to continue logging and also ready to forthcoming JVM shutdown.

all other components of system still can use logback
for logging during JVM/application shutdown process.

here is only one unobvious nuance: how to transparently
switch each logback AsyncAppender from async to sync mode
without messages lost and without messages reordering in log.

bit I am almost sure this is possible to do using java
without perceptible performance degrade for AsyncAppender
when it work in normal (async) mode.

================================================

IMHO, this is the most robust and most useful way to almost completly 
prevent any log messages lost during application and JVM shutdown.

LoggerContext.stop() not prevent messages lost,
so it should not be called before JVM shutdown.

as I understand it mostly will be useful only for web applications,
to be called in ServletContextListener.contextDestroyed() callback.

Let me know, what you think about this new proposed solution.

-- 
Best regards,
  Gena


More information about the Logback-user mailing list