spring-boot - 在 Spring Boot 中定义 logback 关闭钩子(Hook)

标签 spring-boot logback

我在 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/

相关文章:

spring-boot - flyway 是否支持 test 文件夹下的 sql 迁移?

java - 为什么当我尝试在数据库中插入新对象时出现错误?

java - AuthenticationSuccessEvent 从未触发

java - Logback AsyncAppender无法打印方法名称和行号

java - 文件附加器中 web.xml 的 logback 值

java - 登录logback时如何处理disk full错误?

logging - Logback JsonLayout 在同一行打印所有日志

spring-mvc - 如何基于http方法隐藏相关端点

java - 将logback.xml带到 jar 外面

gradle - Groovy @Slf4j 注释 : "Class path contains multiple SLF4J bindings"