001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.hook;
015
016import ch.qos.logback.core.util.Duration;
017
018/**
019 * {@link ShutdownHook} implementation that <b>stops</b> the Logback context
020 * after a specified delay. The default delay is 0 ms (zero).
021 * 
022 * <p>Stopping the logback context
023 * </p>
024 *
025 * @author Mike Reinhold
026 */
027public class DefaultShutdownHook extends ShutdownHookBase {
028    /**
029     * The default is no delay before shutdown.
030     */
031    public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
032
033    /**
034     * The delay in milliseconds before the ShutdownHook stops the logback context
035     */
036    private Duration delay = DEFAULT_DELAY;
037
038
039    /**
040     * Creates a DefaultShutdownHook using the default delay ({@link #DEFAULT_DELAY}).
041     */
042    public DefaultShutdownHook() {
043    }
044
045
046    public Duration getDelay() {
047        return delay;
048    }
049
050    /**
051     * The duration to wait before shutting down the current logback context.
052     *
053     * @param delay
054     */
055    public void setDelay(Duration delay) {
056        this.delay = delay;
057    }
058
059    public void run() {
060        if (delay.getMilliseconds() > 0) {
061            addInfo("Sleeping for " + delay);
062            try {
063                Thread.sleep(delay.getMilliseconds());
064            } catch (InterruptedException e) {
065            }
066        }
067        super.stop();
068    }
069
070}