My service have a log every hour. The active log file is service.log . And the archive file name is like service-2014-12-15-03.log.gz However, if I stop the service on 10:59 and start on 11:01, the service continue to write to service.log and will only do a rotation at 11:59 and have service-2014-12-15-11.log.gz that contains 2 hours of logs. I want to force a log rotation when the service shut down.
I believe my question consist of 3 parts:
- How to hook into shutdown of a Dropwizard service?
- How to get access to the Logback instance (I may be completely wrong here)?
- How to force a log rotation?
I Googled for several hours but found no answer at all. I also looked into Dropwizard source code on GitHub.
Please help. I'm on Dropwizard 0.6.2 but I can upgrade if solutions exists in 0.7.x
====Updates====
The shutdown hook in main method works. The managed objects stop also works. And FYI the managed objects stop get called after the shutdown hook.
^Cshutdown Hook called
INFO [2014-12-17 20:35:07,861] org.eclipse.jetty.server.Server: Graceful shutdown SocketConnector@0.0.0.0:9051
INFO [2014-12-17 20:35:07,862] org.eclipse.jetty.server.Server: Graceful shutdown InstrumentedBlockingChannelConnector@0.0.0.0:9050
INFO [2014-12-17 20:35:07,863] org.eclipse.jetty.server.Server: Graceful shutdown o.e.j.s.ServletContextHandler{/,null}
INFO [2014-12-17 20:35:07,864] org.eclipse.jetty.server.Server: Graceful shutdown o.e.j.s.ServletContextHandler{/,null}
INFO [2014-12-17 20:35:09,918] org.eclipse.jetty.server.handler.ContextHandler: stopped o.e.j.s.ServletContextHandler{/,null}
INFO [2014-12-17 20:35:09,918] org.eclipse.jetty.server.handler.ContextHandler: stopped o.e.j.s.ServletContextHandler{/,null}
Managed object stop called
I still need help on problem 2 and 3.