3

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:

  1. How to hook into shutdown of a Dropwizard service?
  2. How to get access to the Logback instance (I may be completely wrong here)?
  3. 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.

Gordon Sun
  • 278
  • 1
  • 19

2 Answers2

1

I was also looking for the way to close resource when dropwizard application stops running. I have register a shutdown hook in dropwizard application class (i.e the class from where your application starts having main method).

        Runtime.getRuntime().addShutdownHook(new Thread() {
          public void run() {
                System.out.println("shutdown Hook called");
                //database.close();
          }
        });

Look here to force logback rotation. If this works you do not need to have worry about getting logback instance.

EDIT :

This link will help you further, the only difference is they rotate at the application start up rather at shutdown time.

Community
  • 1
  • 1
Kartik Jajal
  • 732
  • 4
  • 10
  • 16
  • I added this to the main method of my service and it worked like a charm. Feels very strange like I'm writing JavaScript. This definitely solves problem 1. But I still do not know how to get access to the file logger and force a log rotation even after I read the link you gave me. I can't figure out how to overwrite the trigger and I do not think Dropwizard has logback.xml for me to edit. – Gordon Sun Dec 17 '14 at 20:12
  • Using the managed objects also works and that got called after the shutdown hook. – Gordon Sun Dec 17 '14 at 20:37
  • Thank you Kartik. The second link you provided is still very different from my situation. I'm in Dropwizard. I do not have the logback.xml file to modify. I do not know how to overwrite the trigger policy. – Gordon Sun Dec 17 '14 at 22:03
  • still doesnt answer the question. – stantonk Jan 04 '16 at 21:34
-1

You can use dropwizards managed objects to register components that need execute on start/stop within the dropwizard lifecycle.

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
  • This also works. However this still only solves problem 1. I still need help with problem 2 and 3. – Gordon Sun Dec 17 '14 at 20:36
  • I just referred to the "setShutdownHook" answer, did not see the other two aspects. Might be a good indicator though that this should have been two different questions (at least). I cannot help you with logback, sorry. – Jan Galinski Dec 18 '14 at 09:56
  • doesnt answer the question – stantonk Jan 04 '16 at 21:34