我在 spring-boot (1.5.3.RELEASE) 应用程序中使用 AsyncAppender。
logback.xml
<appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>5000</queueSize>
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE" />
</appender>
根据 logback 文档,
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
此外,它说:
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
我想知道如何在 Spring Boot 应用程序中停止 AsyncAppender。我应该在 Spring Boot 的哪个位置定义关闭 Hook ?
最佳答案
我遇到了类似的问题,但使用的是 1.1.6 版的 logback 和 spring-boot 1.5.4。
我的解决方案是添加:
logging.register-shutdown-hook=true
到 application.properties 文件进行邀请
org.springframework.boot.logging.logback.LogbackLoggingSystem
在 ApplicationContext.close 调用上停止 LoggerContext。
关于spring-boot - 在 Spring Boot 中定义 logback 关闭钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693107/