From cf99afa33795be107b54ca3d17a79966c053fdd6 Mon Sep 17 00:00:00 2001 From: Johny Muffin Date: Sat, 30 Mar 2024 13:12:14 +1000 Subject: [PATCH] Make latest log file optional --- .../poseidon/PoseidonConfig.java | 25 ++++++++++++----- .../poseidon/util/ServerLogRotator.java | 14 +++++----- .../minecraft/server/ConsoleLogManager.java | 27 +++++++++++++------ .../net/minecraft/server/MinecraftServer.java | 2 +- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/legacyminecraft/poseidon/PoseidonConfig.java b/src/main/java/com/legacyminecraft/poseidon/PoseidonConfig.java index 4a3e2779..f96639c3 100644 --- a/src/main/java/com/legacyminecraft/poseidon/PoseidonConfig.java +++ b/src/main/java/com/legacyminecraft/poseidon/PoseidonConfig.java @@ -9,7 +9,7 @@ public class PoseidonConfig extends Configuration { private static PoseidonConfig singleton; - private final int configVersion = 3; + private final int configVersion = 4; private Integer[] treeBlacklistIDs; public Integer[] getTreeBlacklistIDs() { @@ -38,16 +38,22 @@ public void resetConfig() { private void write() { if (this.getString("config-version") == null || Integer.valueOf(this.getString("config-version")) < configVersion) { - System.out.println("Converting to Config Version: " + configVersion); + System.out.println("[Poseidon] Converting from config version " + (this.getString("config-version") == null ? "0" : this.getString("config-version")) + " to " + configVersion); convertToNewConfig(); + this.setProperty("config-version", configVersion); } //Main - generateConfigOption("config-version", 3); + generateConfigOption("config-version", configVersion); //Setting generateConfigOption("settings.allow-graceful-uuids", true); generateConfigOption("settings.delete-duplicate-uuids", false); generateConfigOption("settings.save-playerdata-by-uuid", true); - generateConfigOption("settings.per-day-logfile", false); + // Log management and rotation + generateConfigOption("settings.per-day-log-file.info", "This setting causes the server to create a new log file each day. This is useful for log rotation and log file management."); + generateConfigOption("settings.per-day-log-file.enabled", false); + generateConfigOption("settings.per-day-log-file.latest-log.info", "This setting causes the server to create a latest.log similar to modern Minecraft servers. This can be useful for certain control panels and log file management."); + generateConfigOption("settings.per-day-log-file.latest-log.enabled", true); + generateConfigOption("settings.fetch-uuids-from", "https://api.mojang.com/profiles/minecraft"); generateConfigOption("settings.remove-join-leave-debug", true); generateConfigOption("settings.enable-tpc-nodelay", false); @@ -237,8 +243,14 @@ private void convertToNewConfig() { convertToNewAddress("settings.statistics.enabled", "settings.enable-statistics"); convertToNewAddress("settings.allow-graceful-uuids", "allowGracefulUUID"); convertToNewAddress("settings.save-playerdata-by-uuid", "savePlayerdataByUUID"); + convertToNewAddress("settings.watchdog.enable", "settings.enable-watchdog"); + // 3-4 Conversion - convertToNewAddress("settings.enable-watchdog", "settings.watchdog.enable"); + // Don't automatically enable the latest log file for servers that have the per-day-logfile setting enabled as this is a change in behavior + if(this.getString("settings.per-day-logfile") != null && this.getConfigBoolean("settings.per-day-logfile")) { + this.setProperty("settings.per-day-log-file.latest-log.enabled", false); + } + convertToNewAddress("settings.per-day-log-file.enabled", "settings.per-day-logfile"); } private boolean convertToNewAddress(String newKey, String oldKey) { @@ -246,9 +258,10 @@ private boolean convertToNewAddress(String newKey, String oldKey) { return false; } if (this.getString(oldKey) == null) { + System.out.println("[Poseidon] Config: " + oldKey + " does not exist. Skipping conversion."); return false; } - System.out.println("Converting Config: " + oldKey + " to " + newKey); + System.out.println("[Poseidon] Converting Config: " + oldKey + " to " + newKey); Object value = this.getProperty(oldKey); this.setProperty(newKey, value); this.removeProperty(oldKey); diff --git a/src/main/java/com/legacyminecraft/poseidon/util/ServerLogRotator.java b/src/main/java/com/legacyminecraft/poseidon/util/ServerLogRotator.java index 5d7730b4..a2935498 100644 --- a/src/main/java/com/legacyminecraft/poseidon/util/ServerLogRotator.java +++ b/src/main/java/com/legacyminecraft/poseidon/util/ServerLogRotator.java @@ -62,7 +62,7 @@ private void archiveLine(String[] parts) { // catch any exceptions that occur during the process, and log them. IOExceptions are possible when calling createNewFile() } catch (IOException e) { - logger.log(Level.SEVERE, "Failed to create new log file!"); + logger.log(Level.SEVERE, "[Poseidon] Failed to create new log file!"); logger.log(Level.SEVERE, e.toString()); } } @@ -73,13 +73,13 @@ private void archiveLine(String[] parts) { */ private void buildHistoricalLogsFromLatestLogFile() { - logger.log(Level.INFO, "Building logs from latest.log..."); + logger.log(Level.INFO, "[Poseidon] Building logs from latest.log..."); try { // open latest log file File latestLog = new File("." + File.separator + "logs" + File.separator + this.latestLogFileName + ".log"); if (!latestLog.exists()) { - logger.log(Level.INFO, "No logs to build from latest.log!"); + logger.log(Level.INFO, "[Poseidon] No logs to build from latest.log!"); return; } @@ -118,11 +118,11 @@ private void buildHistoricalLogsFromLatestLogFile() { writer.print(todayLogs); writer.close(); - logger.log(Level.INFO, "Logs built from latest.log!"); + logger.log(Level.INFO, "[Poseidon] Logs built from latest.log!"); // catch any exceptions that occur during the process, and log them } catch (Exception e) { - logger.log(Level.SEVERE, "Failed to build logs from latest.log!"); + logger.log(Level.SEVERE, "[Poseidon] Failed to build logs from latest.log!"); logger.log(Level.SEVERE, e.toString()); } } @@ -141,8 +141,8 @@ public void start() { buildHistoricalLogsFromLatestLogFile(); // Schedule the log rotation task to run every day at midnight offset by one second to avoid missing logs - logger.log(Level.INFO, "Log rotation task scheduled for run in " + initialDelay + " seconds, and then every " + period + " seconds."); - logger.log(Level.INFO, "If latest.log contains logs from earlier, not previously archived dates, they will be archived to the appropriate log files " + + logger.log(Level.INFO, "[Poseidon] Log rotation task scheduled for run in " + initialDelay + " seconds, and then every " + period + " seconds."); + logger.log(Level.INFO, "[Poseidon] If latest.log contains logs from earlier, not previously archived dates, they will be archived to the appropriate log files " + "upon first run of the log rotation task. If log files already exist for these dates, the logs will be appended to the existing log files!"); Bukkit.getScheduler().scheduleAsyncRepeatingTask(new PoseidonPlugin(), this::buildHistoricalLogsFromLatestLogFile, (initialDelay + 1) * 20, period * 20); } diff --git a/src/main/java/net/minecraft/server/ConsoleLogManager.java b/src/main/java/net/minecraft/server/ConsoleLogManager.java index 3edb5464..3129a748 100644 --- a/src/main/java/net/minecraft/server/ConsoleLogManager.java +++ b/src/main/java/net/minecraft/server/ConsoleLogManager.java @@ -1,11 +1,12 @@ package net.minecraft.server; import com.legacyminecraft.poseidon.PoseidonConfig; -import com.legacyminecraft.poseidon.util.ServerLogRotator; import org.bukkit.craftbukkit.util.ShortConsoleLogFormatter; import org.bukkit.craftbukkit.util.TerminalConsoleHandler; import java.io.File; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.logging.*; // CraftBukkit start @@ -40,13 +41,23 @@ public static void init(MinecraftServer server) { try { //Project Poseidon Start FileHandler filehandler; - if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-logfile")) { - String latestLogFileName = "latest"; - File log = new File("." + File.separator + "logs" + File.separator); - log.getParentFile().mkdirs(); - log.mkdirs(); - filehandler = new FileHandler("." + File.separator + "logs" + File.separator + latestLogFileName + ".log", true); - + if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-log-file.enabled")) { + //If latest log file is enabled, create a new log file for each day + if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-log-file.latest-log.enabled")) { + String latestLogFileName = "latest"; + File log = new File("." + File.separator + "logs" + File.separator); + log.getParentFile().mkdirs(); + log.mkdirs(); + filehandler = new FileHandler("." + File.separator + "logs" + File.separator + latestLogFileName + ".log", true); + } else { + //If latest log file is disabled, create a new log file for each day with the date as the file name + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String logfile = LocalDate.now().format(formatter); + File log = new File("." + File.separator + "logs" + File.separator); + log.getParentFile().mkdirs(); + log.mkdirs(); + filehandler = new FileHandler("." + File.separator + "logs" + File.separator + logfile + ".log", true); + } } else { // CraftBukkit start String pattern = (String) server.options.valueOf("log-pattern"); diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 63eb9e3e..31f7c44b 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -199,7 +199,7 @@ private boolean init() throws UnknownHostException { // CraftBukkit - added thro log.info("Done (" + time + ")! For help, type \"help\" or \"?\""); // log rotator process start. - if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-logfile")) { + if ((boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-log-file.enabled") && (boolean) PoseidonConfig.getInstance().getConfigOption("settings.per-day-log-file.latest-log.enabled")) { String latestLogFileName = "latest"; ServerLogRotator serverLogRotator = new ServerLogRotator(latestLogFileName); serverLogRotator.start();