0

How can i get log4j to log to a file on application exit. currently i am using the below when application is starting, but not sure how to capture on application exit

logger.info("Starting application....");
Ossama
  • 2,333
  • 5
  • 41
  • 75

2 Answers2

3

Use a shutdown hook:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // log here
    });
Bohemian
  • 389,931
  • 88
  • 552
  • 692
2

You'll have to register a shutdown hook

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {            
    @Override
    public void run() {
        // do your thing
    }
}));

Note that this will only get executed if the program ends naturally.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702