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?
Asked
Active
Viewed 625 times
1 Answers
2
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

glytching
- 44,936
- 9
- 114
- 120
-
thanks for your answer! I will test it. A minor comment about it, today's last downloadable version of 1.1 logback series is 1.1.10. There is no 1.1.13 version (yet). Look at [download dist page](https://logback.qos.ch/dist/) – guindous Aug 02 '17 at 11:58
-
Apologies, I referred to 1.1.3 and then 1.1.13 in my answer. The correct version (i.e. the version in which the shutdownHook was introduced) is 1.1.3. so it is therefore available in 1.1.10. And since you are using 1.1.10 you won't need to upgrade just follow the instructions I supplied for defining the shutdownHook in logback.groovy. I have updated my answer. – glytching Aug 02 '17 at 12:12