From 49f7e476457565bdff593011629b589314a84f31 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 5 Feb 2017 13:09:38 +0100 Subject: [PATCH] Add more debug log flavors to ConsoleLogger --- .../java/fr/xephi/authme/ConsoleLogger.java | 94 +++++++++++++++++-- .../fr/xephi/authme/ConsoleLoggerTest.java | 36 ++++++- 2 files changed, 117 insertions(+), 13 deletions(-) diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index b0167459..9d18820c 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -12,7 +12,10 @@ import java.io.FileWriter; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; +import java.util.function.Supplier; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -89,6 +92,18 @@ public final class ConsoleLogger { writeLog("[WARN] " + message); } + /** + * Log a Throwable with the provided message on WARNING level + * and save the stack trace to the log file. + * + * @param message The message to accompany the exception + * @param th The Throwable to log + */ + public static void logException(String message, Throwable th) { + warning(message + " " + StringUtils.formatException(th)); + writeLog(Throwables.getStackTraceAsString(th)); + } + /** * Log an INFO message. * @@ -114,6 +129,10 @@ public final class ConsoleLogger { } } + // -------- + // Debug log methods + // -------- + /** * Log a DEBUG message if enabled. *

@@ -124,21 +143,78 @@ public final class ConsoleLogger { */ public static void debug(String message) { if (logLevel.includes(LogLevel.DEBUG)) { - logger.info("Debug: " + message); - writeLog("[DEBUG] " + message); + String debugMessage = "[DEBUG] " + message; + logger.info(debugMessage); + writeLog(debugMessage); } } /** - * Log a Throwable with the provided message on WARNING level - * and save the stack trace to the log file. + * Log the DEBUG message from the supplier if enabled. * - * @param message The message to accompany the exception - * @param th The Throwable to log + * @param msgSupplier the message supplier */ - public static void logException(String message, Throwable th) { - warning(message + " " + StringUtils.formatException(th)); - writeLog(Throwables.getStackTraceAsString(th)); + public static void debug(Supplier msgSupplier) { + if (logLevel.includes(LogLevel.DEBUG)) { + String debugMessage = "[DEBUG] " + msgSupplier.get(); + logger.info(debugMessage); + writeLog(debugMessage); + } + } + + /** + * Log the DEBUG message. + * + * @param message the message + * @param param1 parameter to replace in the message + */ + public static void debug(String message, String param1) { + if (logLevel.includes(LogLevel.DEBUG)) { + String debugMessage = "[DEBUG] " + message; + logger.log(Level.INFO, debugMessage, param1); + writeLog(debugMessage + " {" + param1 + "}"); + } + } + + /** + * Log the DEBUG message. + * + * @param message the message + * @param param1 first param to replace in message + * @param param2 second param to replace in message + */ + // Avoids array creation if DEBUG level is disabled + public static void debug(String message, String param1, String param2) { + if (logLevel.includes(LogLevel.DEBUG)) { + debug(message, new String[]{param1, param2}); + } + } + + /** + * Log the DEBUG message. + * + * @param message the message + * @param params the params to replace in the message + */ + // Equivalent to debug(String, Object...) but avoids conversions + public static void debug(String message, String... params) { + if (logLevel.includes(LogLevel.DEBUG)) { + String debugMessage = "[DEBUG] " + message; + logger.log(Level.INFO, debugMessage, params); + writeLog(debugMessage + " {" + String.join(", ", params) + "}"); + } + } + + /** + * Log the DEBUG message. + * + * @param message the message + * @param params the params to replace in the message + */ + public static void debug(String message, Object... params) { + if (logLevel.includes(LogLevel.DEBUG)) { + debug(message, Arrays.stream(params).map(String::valueOf).toArray(String[]::new)); + } } diff --git a/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java b/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java index 8eae45ec..7d01a7db 100644 --- a/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java +++ b/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java @@ -19,8 +19,10 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -42,9 +44,6 @@ public class ConsoleLoggerTest { @Mock private Logger logger; - @Mock - private Settings settings; - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -106,7 +105,7 @@ public class ConsoleLoggerTest { ConsoleLogger.warning("Encountered a warning"); // then - verify(logger).info("Debug: Created test"); + verify(logger).info("[DEBUG] Created test"); verify(logger).warning("Encountered a warning"); verifyNoMoreInteractions(logger); assertThat(logFile.length(), equalTo(0L)); @@ -137,6 +136,35 @@ public class ConsoleLoggerTest { assertThat(String.join("", loggedLines), containsString(getClass().getCanonicalName())); } + @Test + public void shouldSupportVariousDebugMethods() throws IOException { + // given + ConsoleLogger.setLoggingOptions(newSettings(true, LogLevel.DEBUG)); + + // when + ConsoleLogger.debug("Got {0} entries", "test"); + ConsoleLogger.debug("Player `{0}` is in world `{1}`", "Bobby", "world"); + ConsoleLogger.debug("The {0} is {1} the {2}", "fox", "behind", "chicken"); + ConsoleLogger.debug("{0} quick {1} jump over {2} lazy {3} (reason: {4})", 5, "foxes", 3, "dogs", null); + ConsoleLogger.debug(() -> "Too little too late"); + + // then + verify(logger).log(Level.INFO, "[DEBUG] Got {0} entries", "test"); + verify(logger).log(Level.INFO, "[DEBUG] Player `{0}` is in world `{1}`", new Object[]{"Bobby", "world"}); + verify(logger).log(Level.INFO, "[DEBUG] The {0} is {1} the {2}", new Object[]{"fox", "behind", "chicken"}); + verify(logger).log(Level.INFO, "[DEBUG] {0} quick {1} jump over {2} lazy {3} (reason: {4})", + new Object[]{"5", "foxes", "3", "dogs", "null"}); + verify(logger).info("[DEBUG] Too little too late"); + + List loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8); + assertThat(loggedLines, contains( + containsString("[DEBUG] Got {0} entries {test}"), + containsString("[DEBUG] Player `{0}` is in world `{1}` {Bobby, world}"), + containsString("[DEBUG] The {0} is {1} the {2} {fox, behind, chicken}"), + containsString("[DEBUG] {0} quick {1} jump over {2} lazy {3} (reason: {4}) {5, foxes, 3, dogs, null}"), + containsString("[DEBUG] Too little too late"))); + } + @Test public void shouldHaveHiddenConstructor() { TestHelper.validateHasOnlyPrivateEmptyConstructor(ConsoleLogger.class);