Merge redundant ConsoleLogger#debug overloads

- No need to distinguish between String[] and Object[] because log4j only has methods for Object[]
This commit is contained in:
ljacqu 2017-10-19 20:14:17 +02:00
parent 22e95493de
commit 7d445217d6
2 changed files with 37 additions and 29 deletions

View File

@ -17,6 +17,7 @@ import java.util.Date;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* The plugin's static logger.
@ -168,7 +169,7 @@ public final class ConsoleLogger {
* @param message the message
* @param param1 parameter to replace in the message
*/
public static void debug(String message, String param1) {
public static void debug(String message, Object param1) {
if (logLevel.includes(LogLevel.DEBUG)) {
String debugMessage = "[DEBUG] " + message;
logger.log(Level.INFO, debugMessage, param1);
@ -184,24 +185,9 @@ public final class ConsoleLogger {
* @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) {
public static void debug(String message, Object param1, Object 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) + "}");
debug(message, new Object[]{param1, param2});
}
}
@ -213,7 +199,10 @@ public final class ConsoleLogger {
*/
public static void debug(String message, Object... params) {
if (logLevel.includes(LogLevel.DEBUG)) {
debug(message, Arrays.stream(params).map(String::valueOf).toArray(String[]::new));
String debugMessage = "[DEBUG] " + message;
logger.log(Level.INFO, debugMessage, params);
writeLog(debugMessage + " {"
+ Arrays.stream(params).map(String::valueOf).collect(Collectors.joining(", ")) + "}");
}
}

View File

@ -19,6 +19,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -142,25 +143,22 @@ public class ConsoleLoggerTest {
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("Got {0} entries", 17);
ConsoleLogger.debug("Player `{0}` is in world `{1}`", "Bobby", new World("world"));
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] Got {0} entries", 17);
verify(logger).log(Level.INFO, "[DEBUG] Player `{0}` is in world `{1}`", new Object[]{"Bobby", new World("world")});
verify(logger).log(Level.INFO, "[DEBUG] {0} quick {1} jump over {2} lazy {3} (reason: {4})",
new Object[]{"5", "foxes", "3", "dogs", "null"});
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] Got {0} entries {17}"),
containsString("[DEBUG] Player `{0}` is in world `{1}` {Bobby, w[world]}"),
containsString("[DEBUG] {0} quick {1} jump over {2} lazy {3} (reason: {4}) {5, foxes, 3, dogs, null}"),
containsString("[DEBUG] Too little too late")));
}
@ -176,4 +174,25 @@ public class ConsoleLoggerTest {
given(settings.getProperty(PluginSettings.LOG_LEVEL)).willReturn(logLevel);
return settings;
}
private static final class World {
private final String name;
World(String name) {
this.name = name;
}
@Override
public String toString() {
return "w[" + name + "]";
}
@Override
public boolean equals(Object other) {
if (other instanceof World) {
return Objects.equals(this.name, ((World) other).name);
}
return false;
}
}
}