Reputation: 41
I'm using logback 1.1.10 with groovy configuration. I recently discovered shutdownHook preference but I haven't found the way to enable it using groovy configuration. Is this possible? Or a missing feature?
Upvotes: 3
Views: 714
Reputation: 47865
Logback's shutdownHook was added in v1.1.3 and it can be configured using Groovy. The docs are a bit light on this matter and the xml->groovy translator ignores the shutdownHook but I have verified the inclusion of a shutdown hook via Groovy config as follows:
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"])
This was confirmed by running a Java process which used logback, explicitly pointing to the above configuration by using -Dlogback.configurationFile
and the resulting output clearly shows the shutdown hook being used:
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
Upvotes: 2