[2.8.x] Do not use/recommend shutdownHook in Logback, Play handles that #11532
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This reverts the changes from #8407 / #8277.
I am sure we don't want logback to shut down the logger context by using
<shutdownHook />(anymore), because Play already handlest that (at least since v2.7). Actually with<shutdownHook />in place, a Play app closes the logger context to early and logs are lost (!)...So what does
<shutdownHook />actually do?It registers a JVM shutdown hoook. So when the JVM shuts down, this hook will automatically close the logger context after a delay... By default that delay is 0, so it will do that immediately. So we end up here and then here.
That might was desirable back when #8407 was merged (but not sure if back then this also made sense), but meanwhile we switched to Akka's Coordinated Shutdown hooks in #8406 (see this line). So now when akka-http or netty shutdown, they also stop the logger context, see here, which calls this, which then shuts down the logger context.
So for production mode, that means, that when you add
<shutdownHook />to your logback config, what happens is, that when you signal your application to shutdown, at first the JVM shutdown hook registered by logback runs and shuts down the logger context. Only later the Akka Coordinated Shutdown hook kicks in (but then the context is closed already).The bad thing about that is that when logbacks hooks runs, a Play app didn't finish it's shutdown yet, so if there are log statements (which there are), they will be lost.
You can easily reproduce that:
I also debugged this behaviour thoroughly: I am 100% sure both stop hooks always call the stop method on the same object, both calls ending up this method (It does not fail when close is called twice, it does not care).
So I am 100%, in a Play app,
<shutdownHook />should never bet set, because it will alway be closed by the backend server.(for dev mode that's a different story, see #10939, but unrelated to
<shutdownHook />, which isn't set in Play's default dev logback config)