如何以编程方式告诉 Logback 重新加载配置

发布于 2025-01-06 00:16:25 字数 116 浏览 2 评论 0原文

我知道有一个 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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2025-01-13 00:16:25

这是 JMXConfigurator.reloadDefaultConfiguration() 的源代码:

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

在需要的地方运行此代码怎么样?

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

(LoggerContext)LoggerFactory.getILoggerFactory()

不幸的是,似乎没有精心设计的 API 可以执行此操作,那么如何提高 问题?您还知道 Logback 具有内置的自动刷新功能吗?

This is the source code of 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?

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

(LoggerContext)LoggerFactory.getILoggerFactory()

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?

肩上的翅膀 2025-01-13 00:16:25

我为此目的使用了以下代码:

public static void reloadLogger() {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

    ContextInitializer ci = new ContextInitializer(loggerContext);
    URL url = ci.findURLOfDefaultConfigurationFile(true);

    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        loggerContext.reset();
        configurator.doConfigure(url);
    } catch (JoranException je) {
        // StatusPrinter will handle this
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
}

I used the following code for this purpose:

public static void reloadLogger() {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

    ContextInitializer ci = new ContextInitializer(loggerContext);
    URL url = ci.findURLOfDefaultConfigurationFile(true);

    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        loggerContext.reset();
        configurator.doConfigure(url);
    } catch (JoranException je) {
        // StatusPrinter will handle this
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
}
日暮斜阳 2025-01-13 00:16:25

对于 logback 1.5.x 或更高版本,以下内容应该有效

    var loggerCtx = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerCtx.reset();
    var ci =  new ContextInitializer(loggerCtx);
    ci.autoConfig();

With logback 1.5.x or later, the following should work

    var loggerCtx = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerCtx.reset();
    var ci =  new ContextInitializer(loggerCtx);
    ci.autoConfig();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文