1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, 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 v1.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  * ShutdownHook implementation that stops the Logback context after a specified
20  * delay.  The default delay is 0 ms (zero).
21  *
22  * @author Mike Reinhold
23  */

24 public class DelayingShutdownHook extends ShutdownHookBase {
25     /**
26      * The default is no delay before shutdown.
27      */

28     public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
29
30     /**
31      * The delay in milliseconds before the ShutdownHook stops the logback context
32      */

33     private Duration delay = DEFAULT_DELAY;
34
35     public DelayingShutdownHook() {
36     }
37
38     public Duration getDelay() {
39         return delay;
40     }
41
42     /**
43      * The duration to wait before shutting down the current logback context.
44      *
45      * @param delay
46      */

47     public void setDelay(Duration delay) {
48         this.delay = delay;
49     }
50
51     public void run() {
52         addInfo("Sleeping for "+delay);
53         try {
54             Thread.sleep(delay.getMilliseconds());
55         } catch (InterruptedException e) {
56         }
57         super.stop();
58     }
59 }
60