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