<div dir="ltr">Hi Gena,<div><br></div><div>Thank you for your suggestion - it always appreciated to have additional input into a design.</div><div><br></div><div>In my opinion, the solution that Ceki and I have been working on is somewhat simpler and probably easier to implement. The full conversation can be viewed in the pull request comments, but basically we have settled on a small alteration to AsyncAppenderBase in Logback-core that makes the worker.join(long) call have a configurable timeout instead of hardcoded to 1000ms. By doing this, LoggerContext.stop is now guaranteed to flush as many queued events as possible within the specified timeout - which can be tuned via the maxShutdownTime configuration element for AsyncAppender. Additionally, since the maxShutdownTime configuration element has the same semantics as Thread.join(long), using a value of 0 will cause the LoggerContext.stop method to wait for all queued events to flush regardless of how long it takes. This solution is better in my mind because it involves very few code changes and does not alter shutdown semantics for any existing users (no additional calls to change the Logback mode), while still making it possible for AsyncAppender to fully flush its queue by adjusting the timeout.</div>
<div><br></div><div>The second piece of the design that we have been working on is how to allow for flushing the queue when it is not possible to call LoggerContext.stop prior to application exit (such as when an application exits implicitly or from) or when shutdown hooks expect the ability to log events. This is a little more complex, but per Ceki's description in the pull request comments, adding a new action to Joran to allow the installation of a shutdown hook that calls LoggerContext.stop is pretty flexible.</div>
<div><br></div><div>Regards,</div><div><br></div><div>Mike Reinhold</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Mar 18, 2014 at 6:37 AM, Gena Makhomed <span dir="ltr"><<a href="mailto:gmm@csdoc.com" target="_blank">gmm@csdoc.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Re: AsyncAppenderBase not flushing queue during JVM shutdown<br>
<br>
Michael Reinhold,<br>
I can propose better solution of described problem:<br>
<br>
we just need make two modes of work for logback:<br>
<br>
1. normal mode, as it work all time after configuration finishing.<br>
<br>
2. ready to shutdown mode, - fail safe mode, without messages lost.<br>
<br>
switching from "normal mode" to "ready to shutdown mode"<br>
do via JVM shutdown hook, - just one shutdown hook for logback.<br>
<br>
if logback turned from "normal mode" to "ready to shutdown mode",<br>
logback change own behavior in such way:<br>
<br>
1. all appenders turn "immediateFlush = true",<br>
regardless of initial value in logback configuration,<br>
to prevent message lost during forthcoming JVM shutdown.<br>
<br>
2. all AsyncAppenders turns into transparent and sync mode.<br>
if AsyncAppender work in this "transparent and sync mode",<br>
all behavior of<br>
<br>
app -> AsyncAppender -> RollingFileAppender -> file<br>
<br>
completly identical to behaviour of<br>
<br>
app -> RollingFileAppender -> file<br>
<br>
after switching from "normal mode" to "ready to shutdown mode",<br>
JVM shutdown hook finished work and now logback is ready<br>
to continue logging and also ready to forthcoming JVM shutdown.<br>
<br>
all other components of system still can use logback<br>
for logging during JVM/application shutdown process.<br>
<br>
here is only one unobvious nuance: how to transparently<br>
switch each logback AsyncAppender from async to sync mode<br>
without messages lost and without messages reordering in log.<br>
<br>
bit I am almost sure this is possible to do using java<br>
without perceptible performance degrade for AsyncAppender<br>
when it work in normal (async) mode.<br>
<br>
==============================<u></u>==================<br>
<br>
IMHO, this is the most robust and most useful way to almost completly prevent any log messages lost during application and JVM shutdown.<br>
<br>
LoggerContext.stop() not prevent messages lost,<br>
so it should not be called before JVM shutdown.<br>
<br>
as I understand it mostly will be useful only for web applications,<br>
to be called in ServletContextListener.<u></u>contextDestroyed() callback.<br>
<br>
Let me know, what you think about this new proposed solution.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Best regards,<br>
 Gena<br>
______________________________<u></u>_________________<br>
Logback-user mailing list<br>
<a href="mailto:Logback-user@qos.ch" target="_blank">Logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" target="_blank">http://mailman.qos.ch/mailman/<u></u>listinfo/logback-user</a><br>
</font></span></blockquote></div><br></div>