我正在使用带有groovy配置的logback 1.1.10。我最近发现shutdownHook preference但我还没有找到使用groovy配置启用它的方法。这可能吗?还是缺少一个功能?
答案 0 :(得分:0)
在v1.1.3中添加了Logback的shutdownHook,可以使用Groovy进行配置。 docs对此问题有点了解,xml->groovy translator忽略了shutdownHook,但我已通过Groovy配置验证了包含关闭挂钩,如下所示:
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
显式指向上述配置,结果输出清楚地显示正在使用的关闭挂钩:
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