gpt4 book ai didi

Logback:使用 Groovy 配置启用 shutdownHook

转载 作者:行者123 更新时间:2023-12-04 16:09:25 27 4
gpt4 key购买 nike

我正在使用带有常规配置的 logback 1.1.10。我最近发现了shutdownHook preference但我还没有找到使用 groovy 配置启用它的方法。这可能吗?还是缺失的功能?

最佳答案

Logback 的 shutdownHook在 v1.1.3 中添加,可以使用 Groovy 进行配置。 docs在这件事上有点轻松,xml->groovy translator忽略了 shutdownHook 但我已经通过 Groovy 配置验证了关闭 Hook 的包含,如下所示:

  import ch.qos.logback.classic.AsyncAppender
import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import static ch.qos.logback.classic.Level.INFO

scan("30 seconds")

statusListener(OnConsoleStatusListener)

def shutdownHook() {
def shutdownHook = new ch.qos.logback.core.hook.DelayingShutdownHook();
shutdownHook.setContext(context);

def Thread hookThread = new Thread(shutdownHook, "Logback shutdown hook [" + context.name + "]");
context.putObject("SHUTDOWN_HOOK", hookThread);
Runtime.getRuntime().addShutdownHook(hookThread);
}

shutdownHook();

appender("Console-Appender", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%d|%-5p|%t|%msg%n"
}
}

logger("com", INFO, ["Console-Appender"], false)

root(INFO, ["Console-Appender"])

这通过运行使用 logback 的 Java 进程得到证实,使用 -Dlogback.configurationFile 明确指向上述配置,结果输出清楚地显示正在使用的关闭钩子(Hook):

08:47:47,705 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Setting ReconfigureOnChangeTask scanning period to 30 seconds
08:47:47,728 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
08:47:47,750 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
08:47:47,752 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Naming appender as [Console-Appender]
08:47:47,902 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Setting level of logger [com] to INFO
08:47:47,911 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Attaching appender named [Console-Appender] to Logger[com]
08:47:47,915 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Setting level of logger [ROOT] to INFO
08:47:47,915 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@7ce97ee5 - Attaching appender named [Console-Appender] to Logger[ROOT]
2017-08-01 08:47:48,072|INFO |main|yippee!
Disconnected from the target VM, address: '127.0.0.1:57421', transport: 'socket'
08:47:48,131 |-INFO in ch.qos.logback.core.hook.DelayingShutdownHook@6765f751 - Sleeping for 0 milliseconds
08:47:48,131 |-INFO in ch.qos.logback.core.hook.DelayingShutdownHook@6765f751 - Logback context being closed via shutdown hook

关于Logback:使用 Groovy 配置启用 shutdownHook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45421215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com