使用 logback 关闭时是否需要刷新事件?

发布于 2024-09-19 07:09:15 字数 221 浏览 3 评论 0原文

我们正在将多个 Web 应用程序从 log4j 迁移到 logback。在关闭我们的应用程序时,我们当前调用:

org.apache.log4j.LogManager.shutdown();

应该刷新所有异步日志记录并关闭所有外部资源(文件、套接字)。

logback 中是否有类似的东西,或者它是否在关闭时自动刷新?

麦克风

We are migrating to logback from log4j for several web apps. In the shutdown of our application we currently call:

org.apache.log4j.LogManager.shutdown();

Which is supposed to flush all async logging and close all external resources (files, sockets).

Is there something similar in logback or does it somehow flush automatically on shutdown?

Mike

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

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

发布评论

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

评论(4

花桑 2024-09-26 07:09:41

从版本 1.1.10 开始,当 Web 应用程序停止或重新加载时,logback 负责停止当前的 logback-classic 上下文。

这是更新的文档: https://logback.qos.ch/manual/configuration.html #webShutdownHook

Version 1.1.10 onwards, logback takes care of stopping the current logback-classic context when the web-app is stopped or reloaded.

Here's the updated doc: https://logback.qos.ch/manual/configuration.html#webShutdownHook

灯下孤影 2024-09-26 07:09:37

我不知道像 log4j 这样的整体管理器关闭,但当使用 ServletContextListener 销毁它们的上下文时,我会关闭所有单独的上下文记录器,如下所示:

ContextSelector selector = StaticLoggerBinder.getSingleton().getContextSelector();
LoggerContext context = selector.detachLoggerContext(contextName);
if (context != null) {
    Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    context.reset();
} else {
    System.err.printf("No context named %s was found", contextName);
}

此外, LoggerContext.stop() 是可用的,并且在内部执行一些相同的功能,但我不要使用它,所以我无法评论它是否比重置更好。

I'm not aware of an overall manager shutdown like log4j's but I close all my individual context loggers when their context is destroyed using a ServletContextListener like so:

ContextSelector selector = StaticLoggerBinder.getSingleton().getContextSelector();
LoggerContext context = selector.detachLoggerContext(contextName);
if (context != null) {
    Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    context.reset();
} else {
    System.err.printf("No context named %s was found", contextName);
}

Also, LoggerContext.stop() is svailable and does some of the same functions internally but I don't use it, so I can't comment on whether its better than reset or not.

杀手六號 2024-09-26 07:09:33

似乎只需将 添加到配置中就应该停止上下文。

来自logback 文档

<configuration>
   <!-- in the absence of the class attribute, assume 
   ch.qos.logback.core.hook.DelayingShutdownHook -->
   <shutdownHook/>
  .... 
</configuration>

以及来自DelayingShutdownHook 摘要

ShutdownHook 实现,在指定的延迟后停止 Logback 上下文。默认延迟为 0 毫秒(零)。

It seems that just adding <shutdownHook/> into configuration should stop the context.

From logback docs:

<configuration>
   <!-- in the absence of the class attribute, assume 
   ch.qos.logback.core.hook.DelayingShutdownHook -->
   <shutdownHook/>
  .... 
</configuration>

And from DelayingShutdownHook summary:

ShutdownHook implementation that stops the Logback context after a specified delay. The default delay is 0 ms (zero).

橘虞初梦 2024-09-26 07:09:29

这是一个简单的方法:

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;

...

ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
// Check for logback implementation of slf4j
if (loggerFactory instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) loggerFactory;
    context.stop();
}

Here's a simple approach:

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;

...

ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
// Check for logback implementation of slf4j
if (loggerFactory instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) loggerFactory;
    context.stop();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文