春季@PreDestroy:无日志记录,因为 Logback 停止得太快
在我的项目中,我使用Logback作为日志记录工具。我有以下课程
@Component
class Test {
@PreDestroy
public void destroy() {
try {
...
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).error(e.getLocalizedMessage(), e);
}
}
}
现在,我取消部署了 servlet。发生异常时,Logback 不会打印消息和堆栈跟踪。这是因为 Logback 在 Spring 调用之前正在清理。取消部署 servlet 时,这是第一个(也是最后一个)日志行:destroy()
15:46:19,084 |-INFO in ch.qos.logback.classic.servlet.LogbackServletContextListener@7957fe56 - About to stop ch.qos.logback.classic.LoggerContext [default]
我验证了 Logback 首先通过在 .System.out.println("...");
destroy()
有没有办法解决这个问题?
我的依赖关系:
<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