mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-03 09:10:01 +01:00
Add more debug log flavors to ConsoleLogger
This commit is contained in:
parent
f6c560cbbe
commit
49f7e47645
@ -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.
|
||||
* <p>
|
||||
@ -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<String> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<String> 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);
|
||||
|
Loading…
Reference in New Issue
Block a user