Skip to content

Commit

Permalink
Make latest log file optional
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysB committed Mar 30, 2024
1 parent e2e68b2 commit cf99afa
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
25 changes: 19 additions & 6 deletions src/main/java/com/legacyminecraft/poseidon/PoseidonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -237,18 +243,25 @@ 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) {
if (this.getString(newKey) != null) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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);
}
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/net/minecraft/server/ConsoleLogManager.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/minecraft/server/MinecraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cf99afa

Please sign in to comment.