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;
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;
}

View File

@ -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);
/* 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));
addHandler(cliLogHandler);
logger.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());
super.log(record);
}
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;
}
}