From 3d1f735c1bced7f822733d8a4c0468a0d2fee3ea Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 1 Jun 2016 06:12:22 +0700 Subject: [PATCH] Use FileWriter to write the messages. --- src/main/java/fr/xephi/authme/AuthMe.java | 1 + .../java/fr/xephi/authme/ConsoleLogger.java | 39 ++++++++++++++++--- .../executable/authme/ReloadCommand.java | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 05023a288..f8d2cc578 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -503,6 +503,7 @@ public class AuthMe extends JavaPlugin { // Disabled correctly ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!"); + ConsoleLogger.close(); } // Stop/unload the server/plugin as defined in the configuration diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index 1fab749f1..29b8c5f64 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -6,9 +6,8 @@ import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.util.StringUtils; import java.io.File; +import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -25,6 +24,7 @@ public final class ConsoleLogger { private static boolean enableDebug = false; private static boolean useLogging = false; private static File logFile; + private static FileWriter fileWriter; private ConsoleLogger() { } @@ -40,6 +40,16 @@ public final class ConsoleLogger { public static void setLoggingOptions(NewSetting settings) { ConsoleLogger.useLogging = settings.getProperty(SecuritySettings.USE_LOGGING); ConsoleLogger.enableDebug = !settings.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE); + if (useLogging) { + if (fileWriter == null) { + try { + fileWriter = new FileWriter(logFile, true); + } catch (IOException ignored) { + } + } + } else { + close(); + } } /** @@ -49,6 +59,10 @@ public final class ConsoleLogger { */ public static void info(String message) { logger.info(message); + if (useLogging) { + writeLog(message); + } + } public static void debug(String message) { @@ -83,9 +97,11 @@ public final class ConsoleLogger { dateTime = DATE_FORMAT.format(new Date()); } try { - Files.write(logFile.toPath(), (dateTime + ": " + message + NEW_LINE).getBytes(), - StandardOpenOption.APPEND, - StandardOpenOption.CREATE); + fileWriter.write(dateTime); + fileWriter.write(": "); + fileWriter.write(message); + fileWriter.write(NEW_LINE); + fileWriter.flush(); } catch (IOException ignored) { } } @@ -105,10 +121,21 @@ public final class ConsoleLogger { * Logs a Throwable with the provided message and saves the stack trace to the log file. * * @param message The message to accompany the exception - * @param th The Throwable to log + * @param th The Throwable to log */ public static void logException(String message, Throwable th) { showError(message + " " + StringUtils.formatException(th)); writeStackTrace(th); } + + public static void close() { + if (fileWriter != null) { + try { + fileWriter.flush(); + fileWriter.close(); + fileWriter = null; + } catch (IOException ignored) { + } + } + } } diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java index 48a43f0a7..1698e316d 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java @@ -35,12 +35,12 @@ public class ReloadCommand implements ExecutableCommand { public void executeCommand(CommandSender sender, List arguments, CommandService commandService) { try { settings.reload(); + ConsoleLogger.setLoggingOptions(settings); // We do not change database type for consistency issues, but we'll output a note in the logs if (!settings.getProperty(DatabaseSettings.BACKEND).equals(dataSource.getType())) { ConsoleLogger.info("Note: cannot change database type during /authme reload"); sender.sendMessage("Note: cannot change database type during /authme reload"); } - ConsoleLogger.setLoggingOptions(settings); initializer.performReloadOnServices(); commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS); } catch (Exception e) {