如何以编程方式告知Logback重新加载配置 [英] How do I programmatically tell Logback to Reload Configuration

查看:152
本文介绍了如何以编程方式告知Logback重新加载配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个reloadDefaultConfiguration()jmx操作,但是没有得到MBean的实例并调用此操作,是否有一个Logback api来重新加载默认配置(可选择指定一个日志配置文件路径)?

I know there's a reloadDefaultConfiguration() jmx operation, but without getting an instance of MBean and invoking this operation, is there a Logback api to reload the default configuration (optionally specifying a log configuration file path)?

推荐答案

这是的源代码JMXConfigurator.reloadDefaultConfiguration()

public void reloadDefaultConfiguration() throws JoranException {
  ContextInitializer ci = new ContextInitializer(loggerContext);
  URL url = ci.findURLOfDefaultConfigurationFile(true);
  loggerContext.reset();
  ci.configureByResource(url);
}

如果只需要在任何地方运行此代码怎么样?

What about just running this code wherever you need it?

唯一的问题是 loggerContext 变量。您可以使用以下方式获取它:

The only problem is the loggerContext variable. You can obtain it using:

(LoggerContext)LoggerFactory.getILoggerFactory()

不幸的是,它看起来不像有一个很好的API来做这个,怎么样提高问题?您是否也知道Logback具有内置的自动刷新功能?

Unfortunately it doesn't look like there is well-factored API to do this, what about raising an issue? Also are you aware that Logback has a built-in auto-refreshing feature?

这篇关于如何以编程方式告知Logback重新加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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