Added new Brigadier commands related APIs for bungee/paper/cli + a few javadoc

This commit is contained in:
2022-08-08 01:42:11 +02:00
parent 2f141d5f84
commit a885c224a6
22 changed files with 1337 additions and 317 deletions

View File

@@ -0,0 +1,41 @@
package fr.pandacube.lib.cli.log;
import java.io.PrintStream;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import fr.pandacube.lib.cli.CLI;
import fr.pandacube.lib.util.Log;
import net.md_5.bungee.log.ColouredWriter;
import net.md_5.bungee.log.ConciseFormatter;
import net.md_5.bungee.log.LoggingOutputStream;
public class CLILogger {
private static Logger logger = null;
public 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));
Log.setLogger(logger);
}
return logger;
}
}

View File

@@ -0,0 +1,90 @@
package fr.pandacube.lib.cli.log;
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 static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private BufferedWriter currentFile = null;
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 ignored) {}
}
@Override
public synchronized void flush() {
if (closed) return;
if (currentFile == null) return;
try {
currentFile.flush();
} catch (IOException ignored) {}
}
@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 ignored) {}
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("logs/latest.log", true));
} catch (SecurityException | IOException e) {
reportError("Erreur lors de l'initialisation d'un fichier log", e, ErrorManager.OPEN_FAILURE);
currentFile = null;
}
}
private String getCurrentDay() {
return dateFormat.format(new Date());
}
}