Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-279

Shutdown hook to close logger context

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • None
    • logback-core
    • None

    Description

      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; }

      }

      }

      Attachments

        Activity

          People

            logback-dev Logback dev list
            tuomas_kiviaho Tuomas Kiviaho
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: