Spring @PreDestroy:没有日志记录,因为Logback停止得太早 [英] Spring @PreDestroy: No logging because Logback stops too soon

查看:156
本文介绍了Spring @PreDestroy:没有日志记录,因为Logback停止得太早的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用Logback作为日志记录工具.我有以下课程

In my project, I am using Logback as logging facility. I have the following class

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

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

Now, I undeploy the servlet. When an exception occurs, Logback is not printing the message and stack trace. This is because Logback is cleaning up before the destroy() is called by Spring. When undeploying the servlet, this is the first (and last) log line:

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首先停止.

I verified that Logback stops first by adding a System.out.println("..."); in the destroy().

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

Is there any way to fix this?

我的依赖项:

<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.

Please note that spring-jcl is used to route commons-logging to slf4j (which will route to logback). I am not using jcl-over-slf4j.

推荐答案

有一种记录的方式禁用以禁用LogbackServletContextListener的注册:

There is a documented way to disable to disable the registering of the LogbackServletContextListener:

您可以禁用自动安装 通过设置一个名为LogbackServletContextListener的命名 Web应用程序中的logbackDisableServletContainerInitializer web.xml文件.这是相关的代码段.

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>

请注意,还可以将logbackDisableServletContainerInitializer变量设置为OS环境变量的Java系统属性.本地设置优先,即网络应用优先,系统属性次之,操作系统环境优先.

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.

如果发生这种情况,我想您可能会想要编写自己的关机钩子并停止

I would imagine you'll probably want to write your own shutdown hook if this is the case and stop the LoggerContext

这篇关于Spring @PreDestroy:没有日志记录,因为Logback停止得太早的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆