From db06ab1be9b3ef2068a901a5e4f3e48eb7322b1a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 8 Oct 2023 01:38:52 +0200 Subject: [PATCH] CLI: Delay shutdown of logging system until the end of the stop() method --- .../java/fr/pandacube/lib/cli/CLIApplication.java | 3 +++ .../java/fr/pandacube/lib/cli/log/CLILogger.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java index dde3fc9..91e34d5 100644 --- a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java +++ b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java @@ -2,6 +2,7 @@ package fr.pandacube.lib.cli; import fr.pandacube.lib.cli.commands.CommandAdmin; import fr.pandacube.lib.cli.commands.CommandStop; +import fr.pandacube.lib.cli.log.CLILogger; import fr.pandacube.lib.util.log.Log; import java.util.concurrent.atomic.AtomicBoolean; @@ -91,6 +92,8 @@ public abstract class CLIApplication { Log.severe("Error stopping application " + getName() + " version " + getClass().getPackage().getImplementationVersion(), t); } finally { Log.info("Bye bye."); + + CLILogger.ShutdownHookDelayerLogManager.resetFinally(); if (!Thread.currentThread().equals(shutdownThread)) System.exit(0); } diff --git a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java index 96356f1..7b872c1 100644 --- a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java +++ b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/log/CLILogger.java @@ -13,6 +13,7 @@ import java.io.PrintStream; import java.util.Scanner; import java.util.logging.Handler; import java.util.logging.Level; +import java.util.logging.LogManager; import java.util.logging.Logger; /** @@ -20,8 +21,22 @@ import java.util.logging.Logger; */ public class CLILogger { + static { + System.setProperty("java.util.logging.manager", ShutdownHookDelayerLogManager.class.getName()); + } + private static Logger logger = null; + + public static class ShutdownHookDelayerLogManager extends LogManager { + static ShutdownHookDelayerLogManager instance; + public ShutdownHookDelayerLogManager() { instance = this; } + @Override public void reset() { /* don't reset yet. */ } + private void reset0() { super.reset(); } + public static void resetFinally() { instance.reset0(); } + } + + /** * Initialize and return the logger for this application. * @param cli the CLI instance to use