Improved log related stuff

This commit is contained in:
Marc Baloup 2021-08-23 03:23:16 +02:00
parent 496a5df812
commit cbdf37800a
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 27 additions and 31 deletions

View File

@ -1,8 +1,8 @@
package fr.pandacube.lib.cli; package fr.pandacube.lib.cli;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole; import org.fusesource.jansi.AnsiConsole;
import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.core.util.Log;
@ -39,19 +39,19 @@ public class CLI {
private ConsoleReader reader; private ConsoleReader reader;
private CLILogger logger; private Logger logger;
public CLI() throws IOException { public CLI() throws IOException {
AnsiConsole.systemInstall(); AnsiConsole.systemInstall();
reader = new ConsoleReader(); reader = new ConsoleReader();
reader.setBellEnabled(false); reader.setBellEnabled(false);
reader.setPrompt("\r"+Ansi.ansi().fg(Ansi.Color.MAGENTA)+">"); reader.setPrompt("\r>");
reader.addCompleter(BrigadierDispatcher.instance); reader.addCompleter(BrigadierDispatcher.instance);
// configuration du formatteur pour le logger // configuration du formatteur pour le logger
System.setProperty("net.md_5.bungee.log-date-format", "yyyy-MM-dd HH:mm:ss"); 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; return logger;
} }

View File

@ -3,40 +3,36 @@ package fr.pandacube.lib.cli;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.md_5.bungee.log.ColouredWriter; import net.md_5.bungee.log.ColouredWriter;
import net.md_5.bungee.log.ConciseFormatter; import net.md_5.bungee.log.ConciseFormatter;
import net.md_5.bungee.log.LoggingOutputStream; import net.md_5.bungee.log.LoggingOutputStream;
public class CLILogger extends Logger { public class CLILogger {
private static Logger logger = null;
/* package */ CLILogger(CLI cli) { /* package */ static synchronized Logger getLogger(CLI cli) {
super("CoreLogger", null); if (logger == null) {
setLevel(Level.ALL); logger = Logger.getGlobal();
setUseParentHandlers(false); logger.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
Handler cliLogHandler = new ColouredWriter(cli.getConsoleReader());
cliLogHandler.setFormatter(new ConciseFormatter(true)); Handler cliLogHandler = new ColouredWriter(cli.getConsoleReader());
addHandler(cliLogHandler); cliLogHandler.setFormatter(new ConciseFormatter(true));
logger.addHandler(cliLogHandler);
Handler fileHandler = new DailyLogRotateFileHandler();
fileHandler.setLevel(Level.INFO); Handler fileHandler = new DailyLogRotateFileHandler();
fileHandler.setFormatter(new ConciseFormatter(false)); fileHandler.setLevel(Level.INFO);
addHandler(fileHandler); fileHandler.setFormatter(new ConciseFormatter(false));
logger.addHandler(fileHandler);
System.setErr(new PrintStream(new LoggingOutputStream(this, Level.SEVERE), true));
System.setOut(new PrintStream(new LoggingOutputStream(this, Level.INFO), true)); 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);
}
} }