kala
kala

Reputation: 23

How to make Logback complete compression before my short-lived application exits?

I'm using the rollingPolicy in Logback to gzip the logs. However, as my application is short-lived (run and quit immediately), it randomly can't finish compressing the log file, resulting in some .tmp files. Is there any way to have Logback prevent my application from exiting until compression completes? I've tried to simply switch to AsyncAppender but no luck.

Upvotes: 1

Views: 1570

Answers (1)

glytching
glytching

Reputation: 47955

Since Logback v1.1.3 you can add a ShutdownHook into your configuration. This will instruct Logback to shut itself down gracefully, including completing any in-flight work such as compressing files on rollover.

In XML configuration you add a ShutdownHook by including the following in your logback.xml:

<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>

Alternatively, you can be explicit about your desired delay period (in milliseconds) as follows:

<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook">
    <delay>100</delay>
</shutdownHook>

More details in the docs. Note: the docs incorrectly imply that you do not need to supply a shutdown hook classname. However, you do have to supply a class name (there's an open issue to get the docs fixed).

Upvotes: 3

Related Questions