1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.hook;
15  
16  import ch.qos.logback.core.util.Duration;
17  
18  /**
19   * {@link ShutdownHook} implementation that <b>stops</b> the Logback context
20   * after a specified delay. The default delay is 0 ms (zero).
21   * 
22   * <p>Stopping the logback context
23   * </p>
24   *
25   * @author Mike Reinhold
26   */
27  public class DefaultShutdownHook extends ShutdownHookBase {
28      /**
29       * The default is no delay before shutdown.
30       */
31      public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
32  
33      /**
34       * The delay in milliseconds before the ShutdownHook stops the logback context
35       */
36      private Duration delay = DEFAULT_DELAY;
37  
38  
39      /**
40       * Creates a DefaultShutdownHook using the default delay ({@link #DEFAULT_DELAY}).
41       */
42      public DefaultShutdownHook() {
43      }
44  
45  
46      public Duration getDelay() {
47          return delay;
48      }
49  
50      /**
51       * The duration to wait before shutting down the current logback context.
52       *
53       * @param delay
54       */
55      public void setDelay(Duration delay) {
56          this.delay = delay;
57      }
58  
59      public void run() {
60          if (delay.getMilliseconds() > 0) {
61              addInfo("Sleeping for " + delay);
62              try {
63                  Thread.sleep(delay.getMilliseconds());
64              } catch (InterruptedException e) {
65              }
66          }
67          super.stop();
68      }
69  
70  }