From c0b2237a12fc878ede8a8bbd48c9b4afa8290016 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 23 Apr 2020 23:27:33 +0800 Subject: Split Log4j --- .../java/com/keuin/kbackupfabric/KBCommands.java | 6 +++- .../com/keuin/kbackupfabric/KBPluginEvents.java | 15 +++++---- .../com/keuin/kbackupfabric/util/PrintUtil.java | 39 ---------------------- .../keuin/kbackupfabric/worker/BackupWorker.java | 11 +++--- .../keuin/kbackupfabric/worker/RestoreWorker.java | 20 ++++++----- 5 files changed, 33 insertions(+), 58 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 825046d..b93a5c9 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -8,6 +8,8 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.time.LocalDateTime; @@ -23,6 +25,7 @@ public final class KBCommands { private static final int SUCCESS = 1; private static final int FAILED = -1; + private static final Logger LOGGER = LogManager.getLogger(); private static final HashMap backupIndexNameMapper = new HashMap<>(); // index -> backupName private static String restoreBackupNameToBeConfirmed = null; @@ -144,6 +147,7 @@ public final class KBCommands { // Do backup BackupMetadata metadata = new BackupMetadata(System.currentTimeMillis(), backupName); + LOGGER.info("Invoking backup worker ..."); BackupWorker.invoke(context, backupName, metadata); return SUCCESS; } @@ -167,7 +171,7 @@ public final class KBCommands { // Get server MinecraftServer server = context.getSource().getMinecraftServer(); String backupFileName = getBackupFileName(backupName); - debug("Backup file name: " + backupFileName); + LOGGER.debug("Backup file name: " + backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depended on your world size and the disk speed, the restore progress may take seconds or minutes.", true); diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index a1ffcad..3b1901e 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -7,6 +7,8 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.server.ServerStartCallback; import net.fabricmc.fabric.api.registry.CommandRegistry; import net.minecraft.server.MinecraftServer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.io.FileInputStream; @@ -15,8 +17,6 @@ import java.io.ObjectInputStream; import java.text.SimpleDateFormat; import java.util.Date; -import static com.keuin.kbackupfabric.util.PrintUtil.debug; -import static com.keuin.kbackupfabric.util.PrintUtil.info; import static org.apache.commons.io.FileUtils.forceDelete; /** @@ -24,6 +24,9 @@ import static org.apache.commons.io.FileUtils.forceDelete; * It contains all events, including the init event. */ public final class KBPluginEvents implements ModInitializer, ServerStartCallback { + + private static final Logger LOGGER = LogManager.getLogger(); + @Override public void onInitialize() { System.out.println("KBackup: Binding events and commands ..."); @@ -33,7 +36,7 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback @Override public void onStartServer(MinecraftServer server) { - debug("KBackup: Initializing ..."); + LOGGER.debug("KBackup: Initializing ..."); // Update backup suggestion list BackupNameSuggestionProvider.setBackupSaveDirectory(BackupFilesystemUtil.getBackupSaveDirectory(server).getPath()); @@ -52,9 +55,9 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback fileInputStream.close(); // Print metadata - info("Recovered from previous backup:"); - info("Backup Name: " + metadata.getBackupName()); - info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime()))); + LOGGER.info("Recovered from previous backup:"); + LOGGER.info("Backup Name: " + metadata.getBackupName()); + LOGGER.info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime()))); // Delete metadata file if (!metadataFile.delete()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java index de63037..fe98cfa 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java @@ -6,24 +6,12 @@ import net.minecraft.text.LiteralText; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public final class PrintUtil { - private static final Logger LOGGER = LogManager.getLogger(); - - private static final boolean printDebugMessages = true; - private static final boolean printErrorMessages = true; - private static final boolean printInfoMessages = true; - - private static final Object syncDebug = new Object(); - private static final Object syncError = new Object(); - private static final Object syncInfo = new Object(); private static final Object syncMessage = new Object(); private static final Style infoStyle = new Style().setColor(Formatting.WHITE); - private static final Style debugStyle = new Style().setUnderline(true); private static final Style warnStyle = new Style().setColor(Formatting.YELLOW); private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED); @@ -60,31 +48,4 @@ public final class PrintUtil { } return context; } - - public static void debug(String message) { - synchronized (syncDebug) { - if (printDebugMessages) { - //System.out.println(String.format("[DBG] [KB] %s", message)); - LOGGER.debug("[KB][DEBUG] " + message); - } - } - } - - public static void error(String message) { - synchronized (syncError) { - if (printErrorMessages) { - //System.out.println(String.format("[ERR] [KB] %s", message)); - LOGGER.error("[KB][ERROR]" + message); - } - } - } - - public static void info(String message) { - synchronized (syncInfo) { - if (printInfoMessages) { - //System.out.println(String.format("[INF] [KB] %s", message)); - LOGGER.info("[KB][INFO] " + message); - } - } - } } diff --git a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java index 0ca9015..57e59fb 100644 --- a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java +++ b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java @@ -8,6 +8,8 @@ import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.world.World; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.io.IOException; @@ -15,7 +17,6 @@ import java.util.HashMap; import java.util.Map; import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*; -import static com.keuin.kbackupfabric.util.PrintUtil.debug; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; /** @@ -23,6 +24,8 @@ import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; * To invoke this worker, simply call invoke() method. */ public final class BackupWorker implements Runnable { + + private static final Logger LOGGER = LogManager.getLogger(); private final CommandContext context; private final String backupName; private final Map oldWorldsSavingDisabled; @@ -53,9 +56,9 @@ public final class BackupWorker implements Runnable { }); // Force to save all player data and worlds - debug("Saving players ..."); + LOGGER.debug("Saving players ..."); server.getPlayerManager().saveAllPlayerData(); - debug("Saving worlds ..."); + LOGGER.debug("Saving worlds ..."); server.save(true, true, true); // Start threaded worker @@ -82,7 +85,7 @@ public final class BackupWorker implements Runnable { // Make zip String levelPath = getLevelPath(server); String backupFileName = getBackupFileName(backupName); - debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString())); + LOGGER.debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString())); ZipUtil.makeBackupZip(levelPath, backupSaveDirectoryFile.toString(), backupFileName, backupMetadata); File backupZipFile = new File(backupSaveDirectoryFile, backupFileName); diff --git a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java index 9e424d7..8d367d7 100644 --- a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java +++ b/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java @@ -3,11 +3,12 @@ package com.keuin.kbackupfabric.worker; import com.keuin.kbackupfabric.util.ZipUtil; import com.keuin.kbackupfabric.util.ZipUtilException; import net.minecraft.server.MinecraftServer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.File; import java.io.IOException; -import static com.keuin.kbackupfabric.util.PrintUtil.*; import static org.apache.commons.io.FileUtils.forceDelete; /** @@ -15,6 +16,9 @@ import static org.apache.commons.io.FileUtils.forceDelete; * To invoke this worker, simply call invoke() method. */ public final class RestoreWorker implements Runnable { + + private static final Logger LOGGER = LogManager.getLogger(); + private final Thread serverThread; private final String backupFilePath; private final String levelDirectory; @@ -40,7 +44,7 @@ public final class RestoreWorker implements Runnable { public void run() { try { // Wait server thread die - debug("Waiting server thread stopping ..."); + LOGGER.debug("Waiting for the server thread to exit ..."); while (serverThread.isAlive()) { try { serverThread.join(); @@ -48,14 +52,14 @@ public final class RestoreWorker implements Runnable { } } - debug("Wait for 5 seconds ..."); + LOGGER.debug("Wait for 5 seconds ..."); try { Thread.sleep(5000); } catch (InterruptedException ignored) { } // Delete old level - debug("Server stopped. Deleting old level ..."); + LOGGER.debug("Server stopped. Deleting old level ..."); File levelDirFile = new File(levelDirectory); int failedCounter = 0; @@ -75,16 +79,16 @@ public final class RestoreWorker implements Runnable { } } if (levelDirFile.exists()) { - error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); + LOGGER.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); return; } // Decompress archive - debug("Decompressing archived level"); + LOGGER.debug("Decompressing archived level"); ZipUtil.unzip(backupFilePath, levelDirectory, false); - info("Restore complete! Please restart the server manually."); + LOGGER.info("Restore complete! Please restart the server manually."); } catch (SecurityException | IOException | ZipUtilException e) { - error("An exception occurred while restoring: " + e.getMessage()); + LOGGER.error("An exception occurred while restoring: " + e.getMessage()); } } } -- cgit v1.2.3