如何使用Log4j2登录关闭钩子? [英] How to log within shutdown hooks with Log4j2?

查看:266
本文介绍了如何使用Log4j2登录关闭钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Log4j2还使用关闭挂钩来结束它的服务。但是我当然希望在我的应用程序的整个生命周期中记录 - 包括关闭。使用Log4j这没问题。现在似乎不可能。记录关闭,而我的应用程序仍在处理它。有没有人希望我?

Log4j2 also uses shutdown hooks to end it's services. But of course I want to log throughout the whole lifecycle of my application - shutdown included. With Log4j this was no problem. Now it seems to be impossible. Logging shuts down, while my application is still working on it. Has anyone some hope for me?

最好的问候
Martin

Best regards Martin

推荐答案

从2.0-beta9开始,现在可以在xml中配置

As of 2.0-beta9 this is now configurable in xml

<configuration ... shutdownHook="disable">

考虑到它现在被禁用,我想我需要在关机结束时手动关闭日志系统钩。但是我找不到通过外部接口的方法,只能在内部api

Considering its now disabled, I guess I need to manually shutdown the logging system at the end of my shutdown hook. However I couldn't find a means thorough the external interface, only in the internal api

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.LoggerContext;
...

public static void main(String[] args) {
    final AnnotationConfigApplicationContext springContext = new AnnotationConfigApplicationContext(AppConfig.class)

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            //shutdown application
            LOG.info("Shutting down spring context");
            springContext.close();

            //shutdown log4j2
            if( LogManager.getContext() instanceof LoggerContext ) {
                logger.info("Shutting down log4j2");
                Configurator.shutdown((LoggerContext)LogManager.getContext());
            } else
                logger.warn("Unable to shutdown log4j2");
        }
    });

    //more application initialization
}

这篇关于如何使用Log4j2登录关闭钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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