本文介绍了调用LogManager.shutdown()后如何重新启动log4j2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以像这样重新配置log4j2:

I have a method to reconfig log4j2 like this:

LogManager.shutdown(); // SHUTDOWN log4j2
    InputStream in = getClass().getResourceAsStream("/my2ndLog4j2.xml");
    ConfigurationSource source = new ConfigurationSource(in);
    Configurator.initialize(null, source);
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    context.reconfigure();
    context.start(); // NOT WORK, no log appear anymore...

因此,我可以在运行时在生产日志配置和开发配置之间重新配置log4j2,但是当我调用LogManager.shutdown()时,它将永远关机,永不返回,context.start()不起作用,实际上我这样做长时间在log4j1.x中使用,在log4j1.x中使用:

So I can reconfig log4j2 between production log config and development config at runtime, but when I call LogManager.shutdown(), it really SHUTDOWN forever, never get back, context.start() is not work, infact I do this in log4j1.x for long time, in log4j1.x, use:

org.apache.log4j.PropertyConfigurator.configure(cfgPath);

然后又回来了,但是log4j2怎么回事?

then it get back, but how log4j2?

推荐答案

如果要在运行时更改log4j2配置文件,则下面的代码足以完成此操作-

If you want to change log4j2 configuration file at runtime, then below code is sufficient to do so -

Configurator.initialize(null, "my2ndLog4j2.properties");

无需关闭LogManager,然后再重新启动.

There is no need to shutdown LogManager and then starting it again.

这篇关于调用LogManager.shutdown()后如何重新启动log4j2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:31