[logback-user] Q: Howto force flush before exiting

Ceki Gulcu ceki at qos.ch
Wed Jun 20 21:23:37 CEST 2007

Create a custom evaluator which returns true on events of level ERROR *or* on 
events marked with "FLUSH_SMTP".

Before exiting your application you call:

   Marker flush = MarkerFactory.getMarker("FLUSH_SMTP");
   logger.debug(flush, "Application exiting");

Does it make sense?

Jason Buberel wrote:
> Possibly - that is what I was hoping to achieve with hivemodule's Discardable
> interface. However, I could just as easily (were the method 'flush' a public
> method that would trigger the sending of the email) add something like this
> near the end of my application:
> Logger EMAIL_LOGGER = LoggerFactory.getLogger("email");
> EMAIL_LOGGER.flush();
> -jason
> Ceki Gulcu-2 wrote:
>> Hi Jason,
>> Is there a specific point in your application when you know that it is
>> about to 
>> be terminated. Put differently, can you call a specific function (to be 
>> discussed later) just before exiting your app?
>> Jason Buberel wrote:
>>> Here is the behavior I currently observe:
>>> I am using an SMTPAppender to delivery logging messages to my Inbox. By
>>> default, the SMTPAppender implementation that comes with Logback will
>>> trigger the sending of an email (including all accumulated messages) upon
>>> receipt of an ERROR level logging message.
>>> As a result of the default behavior, you can end up in a situation where
>>> you
>>> generate 100 INFO level messages to the SMTPAppender and when your
>>> application exits, no email will have been sent. Those 100 INFO-level
>>> logging messages will be discarded.
>>> Here is the behavior I would like to achieve:
>>> I would like to implement an EventEvaluator or TriggerPolicy that would
>>> force the email to be sent at time of exit or termination. I know that
>>> overriding finalize() is not a reliable method of doing this, as there is
>>> no
>>> guarantee that the VM will actually call it.
>>> I did try using hivemodule's Discardable interface in a subclass of
>>> SMTPAppender, which I use elsewhere in my application, but ran into
>>> problems
>>> with class loaders and the like.
>>> Any suggestions on the best way to achieve this - guaranteed flush/send
>>> of
>>> the email message if there are any unsent events in the queue? 
>>> -jason
>> -- 
>> Ceki Gülcü
>> Logback: The reliable, generic, fast and flexible logging framework for
>> Java.
>> http://logback.qos.ch
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch
>> http://qos.ch/mailman/listinfo/logback-user

Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.

More information about the Logback-user mailing list