From 30bdc8478cf51c2d196ba036badc9c8cabace340 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 23 Aug 2021 02:23:01 +0200 Subject: [PATCH] Improved CLI/standalone app related stuff --- .../cli/{ConsoleInterface.java => CLI.java} | 77 +++------------- .../cli/{CoreLogger.java => CLILogger.java} | 19 +++- .../lib/cli/DailyLogRotateFileHandler.java | 91 +++++++++++++++++++ 3 files changed, 120 insertions(+), 67 deletions(-) rename CLI/src/main/java/fr/pandacube/lib/cli/{ConsoleInterface.java => CLI.java} (62%) rename CLI/src/main/java/fr/pandacube/lib/cli/{CoreLogger.java => CLILogger.java} (53%) create mode 100644 CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/ConsoleInterface.java b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java similarity index 62% rename from CLI/src/main/java/fr/pandacube/lib/cli/ConsoleInterface.java rename to CLI/src/main/java/fr/pandacube/lib/cli/CLI.java index f6fac5a..c6829ba 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/ConsoleInterface.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java @@ -1,16 +1,14 @@ package fr.pandacube.lib.cli; import java.io.IOException; -import java.io.PrintWriter; -import java.util.logging.ErrorManager; -import java.util.logging.Handler; -import java.util.logging.LogRecord; + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.AnsiConsole; import fr.pandacube.lib.core.util.Log; import jline.console.ConsoleReader; -import net.md_5.bungee.log.ConciseFormatter; -public class ConsoleInterface extends Handler { +public class CLI { public static final String ANSI_RESET = "\u001B[0m"; @@ -41,20 +39,19 @@ public class ConsoleInterface extends Handler { private ConsoleReader reader; - private PrintWriter out; + private CLILogger logger; - - public ConsoleInterface() throws IOException { + public CLI() throws IOException { + AnsiConsole.systemInstall(); reader = new ConsoleReader(); reader.setBellEnabled(false); - reader.setPrompt("\r"+ANSI_LIGHT_PURPLE+">"); - out = new PrintWriter(reader.getOutput()); + reader.setPrompt("\r"+Ansi.ansi().fg(Ansi.Color.MAGENTA)+">"); 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"); - setFormatter(new ConciseFormatter(true)); + logger = new CLILogger(this); } @@ -65,6 +62,11 @@ public class ConsoleInterface extends Handler { } + public CLILogger getLogger() { + return logger; + } + + public void loop() { @@ -85,59 +87,8 @@ public class ConsoleInterface extends Handler { Log.severe(e); } - - - - - - } - - private synchronized void println(String str) { - try { - out.println('\r'+ANSI_RESET+str); - out.flush(); - reader.drawLine(); - reader.flush(); - } catch (IOException e) { - Log.severe(e); - } - } - - - - - - @Override - public void close() throws SecurityException { } - - @Override - public void flush() { } - - @Override - public void publish(LogRecord record) { - if (!isLoggable(record)) - return; - - String formattedMessage; - - try { - formattedMessage = getFormatter().format(record); - } catch (Exception ex) { - reportError(null, ex, ErrorManager.FORMAT_FAILURE); - return; - } - - try { - println(formattedMessage.trim()); - } catch (Exception ex) { - reportError(null, ex, ErrorManager.WRITE_FAILURE); - return; - } - } - - } diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/CoreLogger.java b/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java similarity index 53% rename from CLI/src/main/java/fr/pandacube/lib/cli/CoreLogger.java rename to CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java index f8bbbaf..794ce75 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/CoreLogger.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/CLILogger.java @@ -1,25 +1,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 CoreLogger extends Logger { +public class CLILogger extends Logger { - public CoreLogger(ConsoleInterface cli) { + /* package */ CLILogger(CLI cli) { super("CoreLogger", null); setLevel(Level.ALL); setUseParentHandlers(false); - addHandler(cli); + + 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)); } - @Override public void log(LogRecord record) { record.setLongThreadID(Thread.currentThread().getId()); diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java b/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java new file mode 100644 index 0000000..5f1d01a --- /dev/null +++ b/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java @@ -0,0 +1,91 @@ +package fr.pandacube.lib.cli; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.logging.ErrorManager; +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +class DailyLogRotateFileHandler extends Handler { + + private BufferedWriter currentFile = null; + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + private String currentFileDate = getCurrentDay(); + private boolean closed = false; + + + @Override + public synchronized void close() throws SecurityException { + closed = true; + if (currentFile != null) try { + currentFile.close(); + } catch (IOException e) {} + } + + @Override + public synchronized void flush() { + if (closed) return; + if (currentFile == null) return; + try { + currentFile.flush(); + } catch (IOException e) {} + } + + @Override + public synchronized void publish(LogRecord record) { + if (closed) return; + if (!isLoggable(record)) return; + + if (currentFile == null || !currentFileDate.equals(getCurrentDay())) changeFile(); + + if (currentFile == null) return; + + String formattedMessage; + + try { + formattedMessage = getFormatter().format(record); + } catch (Exception ex) { + reportError(null, ex, ErrorManager.FORMAT_FAILURE); + return; + } + + try { + currentFile.write(formattedMessage); + currentFile.flush(); + } catch (Exception ex) { + reportError(null, ex, ErrorManager.WRITE_FAILURE); + + } + } + + private void changeFile() { + if (currentFile != null) { + try { + currentFile.flush(); + currentFile.close(); + } catch (IOException e) {} + new File("logs/latest.log").renameTo(new File("logs/" + currentFileDate + ".log")); + } + + currentFileDate = getCurrentDay(); + try { + File logDir = new File("logs"); + logDir.mkdir(); + currentFile = new BufferedWriter(new FileWriter(new File("logs/latest.log"), true)); + } catch (SecurityException | IOException e) { + reportError("Erreur lors de l'initialisation d'un fichier log", e, ErrorManager.OPEN_FAILURE); + currentFile = null; + return; + } + + } + + private String getCurrentDay() { + return dateFormat.format(new Date()); + } +} \ No newline at end of file