Improved CLI/standalone app related stuff
This commit is contained in:
parent
3bf4184fd2
commit
30bdc8478c
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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());
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user