From cbdf37800a1c5e94b6edf43ba286201bc42956d5 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 23 Aug 2021 03:23:16 +0200 Subject: [PATCH] Improved log related stuff --- .../main/java/fr/pandacube/lib/cli/CLI.java | 10 ++-- .../java/fr/pandacube/lib/cli/CLILogger.java | 48 +++++++++---------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java index c6829ba..813579d 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java @@ -1,8 +1,8 @@ package fr.pandacube.lib.cli; import java.io.IOException; +import java.util.logging.Logger; -import org.fusesource.jansi.Ansi; import org.fusesource.jansi.AnsiConsole; import fr.pandacube.lib.core.util.Log; @@ -39,19 +39,19 @@ public class CLI { private ConsoleReader reader; - private CLILogger logger; + private Logger logger; public CLI() throws IOException { AnsiConsole.systemInstall(); reader = new ConsoleReader(); reader.setBellEnabled(false); - reader.setPrompt("\r"+Ansi.ansi().fg(Ansi.Color.MAGENTA)+">"); + reader.setPrompt("\r>"); reader.addCompleter(BrigadierDispatcher.instance); // configuration du formatteur pour le logger System.setProperty("net.md_5.bungee.log-date-format", "yyyy-MM-dd HH:mm:ss"); - logger = new CLILogger(this); + logger = CLILogger.getLogger(this); } @@ -62,7 +62,7 @@ public class CLI { } - public CLILogger getLogger() { + public Logger getLogger() { return logger; } diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java b/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java index 794ce75..79d6e33 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java @@ -3,40 +3,36 @@ package fr.pandacube.lib.cli; import java.io.PrintStream; import java.util.logging.Handler; import java.util.logging.Level; -import java.util.logging.LogRecord; import java.util.logging.Logger; import net.md_5.bungee.log.ColouredWriter; import net.md_5.bungee.log.ConciseFormatter; import net.md_5.bungee.log.LoggingOutputStream; -public class CLILogger extends Logger { +public class CLILogger { + private static Logger logger = null; - /* package */ CLILogger(CLI cli) { - super("CoreLogger", null); - setLevel(Level.ALL); - setUseParentHandlers(false); - - Handler cliLogHandler = new ColouredWriter(cli.getConsoleReader()); - cliLogHandler.setFormatter(new ConciseFormatter(true)); - addHandler(cliLogHandler); - - Handler fileHandler = new DailyLogRotateFileHandler(); - fileHandler.setLevel(Level.INFO); - fileHandler.setFormatter(new ConciseFormatter(false)); - addHandler(fileHandler); - - System.setErr(new PrintStream(new LoggingOutputStream(this, Level.SEVERE), true)); - System.setOut(new PrintStream(new LoggingOutputStream(this, Level.INFO), true)); + /* package */ static synchronized Logger getLogger(CLI cli) { + if (logger == null) { + logger = Logger.getGlobal(); + logger.setLevel(Level.ALL); + logger.setUseParentHandlers(false); + + Handler cliLogHandler = new ColouredWriter(cli.getConsoleReader()); + cliLogHandler.setFormatter(new ConciseFormatter(true)); + logger.addHandler(cliLogHandler); + + Handler fileHandler = new DailyLogRotateFileHandler(); + fileHandler.setLevel(Level.INFO); + fileHandler.setFormatter(new ConciseFormatter(false)); + logger.addHandler(fileHandler); + + System.setErr(new PrintStream(new LoggingOutputStream(logger, Level.SEVERE), true)); + System.setOut(new PrintStream(new LoggingOutputStream(logger, Level.INFO), true)); + + } + return logger; } - @Override - public void log(LogRecord record) { - record.setLongThreadID(Thread.currentThread().getId()); - - super.log(record); - } - - }