CLI: Delay shutdown of logging system until the end of the stop() method

This commit is contained in:
Marc Baloup 2023-10-08 01:38:52 +02:00
parent 728961d19f
commit db06ab1be9
2 changed files with 18 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package fr.pandacube.lib.cli;
import fr.pandacube.lib.cli.commands.CommandAdmin; import fr.pandacube.lib.cli.commands.CommandAdmin;
import fr.pandacube.lib.cli.commands.CommandStop; import fr.pandacube.lib.cli.commands.CommandStop;
import fr.pandacube.lib.cli.log.CLILogger;
import fr.pandacube.lib.util.log.Log; import fr.pandacube.lib.util.log.Log;
import java.util.concurrent.atomic.AtomicBoolean; 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); Log.severe("Error stopping application " + getName() + " version " + getClass().getPackage().getImplementationVersion(), t);
} finally { } finally {
Log.info("Bye bye."); Log.info("Bye bye.");
CLILogger.ShutdownHookDelayerLogManager.resetFinally();
if (!Thread.currentThread().equals(shutdownThread)) if (!Thread.currentThread().equals(shutdownThread))
System.exit(0); System.exit(0);
} }

View File

@ -13,6 +13,7 @@ import java.io.PrintStream;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -20,8 +21,22 @@ import java.util.logging.Logger;
*/ */
public class CLILogger { public class CLILogger {
static {
System.setProperty("java.util.logging.manager", ShutdownHookDelayerLogManager.class.getName());
}
private static Logger logger = null; 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. * Initialize and return the logger for this application.
* @param cli the CLI instance to use * @param cli the CLI instance to use