gpt4 book ai didi

java - Spring @PreDestroy : No logging because Logback stops too soon

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:17 28 4
gpt4 key购买 nike

在我的项目中,我使用 Logback 作为日志工具。我有以下类(class)

@Component
class Test {
@PreDestroy
public void destroy() {
try {
...
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).error(e.getLocalizedMessage(), e);
}
}
}

现在,我取消部署 servlet。发生异常时,Logback 不会打印消息和堆栈跟踪。这是因为在 Spring 调用 destroy() 之前,Logback 正在清理。取消部署 servlet 时,这是第一行(也是最后一行)日志行:

15:46:19,084 |-INFO in ch.qos.logback.classic.servlet.LogbackServletContextListener@7957fe56 - About to stop ch.qos.logback.classic.LoggerContext [default]

我通过在 destroy() 中添加 System.out.println("..."); 来验证 Logback 首先停止。

有什么办法可以解决这个问题吗?

我的依赖项:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>99-empty</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</dependency>
</dependencies>

请注意,spring-jcl 用于将 commons-logging 路由到 slf4j(它将路由到 logback)。我没有使用 jcl-over-slf4j

最佳答案

有一个documented way禁用以禁用 LogbackServletContextListener 的注册:

You may disable the automatic the installation of LogbackServletContextListener by setting a named logbackDisableServletContainerInitializer in your web-application's web.xml file. Here is the relevant snippet.

<web-app>
<context-param>
<param-name>logbackDisableServletContainerInitializer</param-name>
<param-value>true</param-value>
</context-param>
....
</web-app>

Note that logbackDisableServletContainerInitializer variable can also be set as a Java system property an OS environment variable. The most local setting has priority, i.e. web-app first, system property second and OS environment last.

如果是这种情况,我想您可能想要编写自己的关闭钩子(Hook)并停止 LoggerContext

关于java - Spring @PreDestroy : No logging because Logback stops too soon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47184045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com