Shutdown hook to close logger context
-------------------------------------

                 Key: LBCORE-202
                 URL: http://jira.qos.ch/browse/LBCORE-202
             Project: logback-core
          Issue Type: Improvement
          Components: Other
    Affects Versions: 0.9.27
            Reporter: Tuomas Kiviaho
            Assignee: Logback dev list


I'm using header and footer with my file appender and would like footers to be 
present in file when processes exit (JUnit tests mainly). I figured out a to 
achieve this by using status listeners and logger context (presented below) 
combined with shutdown hook. This is something that logback jar could probably 
in one form or another (spring framework for instance has similar feature for 
their context).

public class ShutdownHookStatusListener extends ContextAwareBase implements
                StatusListener, LifeCycle {

        private Thread shutdownHook;

        @Override
        public void addStatusEvent(Status status) {

        }

        @Override
        public boolean isStarted() {
                return this.shutdownHook != null;
        }

        @Override
        public void start() {
                if (!this.isStarted()) {
                        this.shutdownHook = new Thread() {

                                @Override
                                public void run() {
                                        LoggerContext loggerContext = 
(LoggerContext) LoggerFactory
                                                        .getILoggerFactory();
                                        loggerContext.stop();
                                }

                        };
                        Runtime runtime = Runtime.getRuntime();
                        runtime.addShutdownHook(this.shutdownHook);
                }
        }

        @Override
        public void stop() {
                if (this.isStarted()) {
                        Runtime runtime = Runtime.getRuntime();
                        runtime.removeShutdownHook(this.shutdownHook);
                        this.shutdownHook = null;
                }
        }

}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.qos.ch/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        
_______________________________________________
logback-dev mailing list
[email protected]
http://qos.ch/mailman/listinfo/logback-dev

Reply via email to