Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define logback shutdown hook in Spring Boot

I am using AsyncAppender in spring-boot (1.5.3.RELEASE) application.

logback.xml

<appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <queueSize>5000</queueSize>
    <discardingThreshold>0</discardingThreshold>
    <appender-ref ref="FILE" />
</appender>

As per logback documentation,

Upon application shutdown or redeploy, AsyncAppender must be stopped in order to stop and reclaim the worker thread and to flush the logging events from the queue.

https://logback.qos.ch/manual/appenders.html

Further it says:

In order to avoid interrupting the worker thread under these conditions, a shutdown hook can be inserted to the JVM runtime that stops the LoggerContext properly after JVM shutdown has been initiated

I want to know how to stop AsyncAppender in Spring Boot application. At which place in Spring Boot, should I define shutdown hook?

like image 876
Moazzam Avatar asked Aug 15 '17 12:08

Moazzam


People also ask

What is shutdown hook in Spring boot?

Each SpringApplication will register a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit. All the standard Spring lifecycle callbacks (such as the DisposableBean interface, or the @PreDestroy annotation) can be used. In addition, beans may implement the org. springframework.

What is shutdown hook in Java?

A special construct that facilitates the developers to add some code that has to be run when the Java Virtual Machine (JVM) is shutting down is known as the Java shutdown hook. The Java shutdown hook comes in very handy in the cases where one needs to perform some special cleanup work when the JVM is shutting down.

What is Logback Spring boot?

Logback is provided out of the box with Spring Boot when you use one of the Spring Boot starter dependencies, as they include spring-boot-starter-logging — providing logging without any configuration that can be altered to work differently if required. There are two ways of providing your own configuration.

What is graceful shutdown in Spring boot?

During a graceful shutdown Spring Boot allows some grace period to the application to finish all the current requests or processes. Once, the grace period is over the unfinished processes or requests are just killed. By default, Spring Boot allows a 30 seconds graceful shutdown timeout.


2 Answers

Just add the <shutdownHook/> instruction to your logback.xml. For example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

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

    <appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <queueSize>5000</queueSize>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="FILE" />
    </appender>

    <!-- the rest of your logback config -->

</configuration>

With this in place you'll notice the following log message is emitted when Logback is configuring itself:

INFO in ch.qos.logback.core.joran.action.ShutdownHookAction - About to instantiate shutdown hook of type [ch.qos.logback.core.hook.DelayingShutdownHook]

like image 94
glytching Avatar answered Nov 07 '22 05:11

glytching


I had a similar problem but with version 1.1.6 of logback and spring-boot 1.5.4. The solution for me was to add :

logging.register-shutdown-hook=true

to the application.properties file for inviting

org.springframework.boot.logging.logback.LogbackLoggingSystem to stop the LoggerContext on ApplicationContext.close invocation.

like image 30
Balaban Mario Avatar answered Nov 07 '22 04:11

Balaban Mario