From 410e1274255c74248cdeb752dae7115cff8a3bf3 Mon Sep 17 00:00:00 2001 From: Sam Ezeh Date: Sun, 1 Oct 2017 11:03:15 +0100 Subject: [PATCH] Allows for the log level to be changed --- src/main/java/cn/nukkit/Nukkit.java | 33 ++++++++++++++- src/main/java/cn/nukkit/utils/LogLevel.java | 10 ++++- src/main/java/cn/nukkit/utils/MainLogger.java | 41 +++++++++++-------- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/main/java/cn/nukkit/Nukkit.java b/src/main/java/cn/nukkit/Nukkit.java index 435374a0a4..03ec56f906 100644 --- a/src/main/java/cn/nukkit/Nukkit.java +++ b/src/main/java/cn/nukkit/Nukkit.java @@ -2,9 +2,14 @@ import cn.nukkit.command.CommandReader; import cn.nukkit.network.protocol.ProtocolInfo; +import cn.nukkit.utils.LogLevel; import cn.nukkit.utils.MainLogger; import cn.nukkit.utils.ServerKiller; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + /** * `_ _ _ _ _ _ * | \ | | | | | | (_) | @@ -50,16 +55,42 @@ public static void main(String[] args) { } } + LogLevel logLevel = LogLevel.DEFAULT_LEVEL; + int index = -1; + boolean skip = false; //启动参数 for (String arg : args) { + index++; + if (skip) { + skip = false; + continue; + } + switch (arg) { case "disable-ansi": ANSI = false; break; + + case "--verbosity": + case "-v": + skip = true; + try { + String levelName = args[index + 1]; + Set levelNames = Arrays.stream(LogLevel.values()).map(level -> level.name().toLowerCase()).collect(Collectors.toSet()); + if (!levelNames.contains(levelName.toLowerCase())) { + System.out.printf("'%s' is not a valid log level, using the default\n", levelName); + continue; + } + logLevel = Arrays.stream(LogLevel.values()).filter(level -> level.name().equalsIgnoreCase(levelName)).findAny().orElse(LogLevel.DEFAULT_LEVEL); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("You must enter the requested log level, using the default\n"); + } + } } - MainLogger logger = new MainLogger(DATA_PATH + "server.log"); + MainLogger logger = new MainLogger(DATA_PATH + "server.log", logLevel); + System.out.printf("Using log level '%s'\n", logLevel); try { if (ANSI) { diff --git a/src/main/java/cn/nukkit/utils/LogLevel.java b/src/main/java/cn/nukkit/utils/LogLevel.java index 9a674f6431..c883031763 100644 --- a/src/main/java/cn/nukkit/utils/LogLevel.java +++ b/src/main/java/cn/nukkit/utils/LogLevel.java @@ -4,7 +4,8 @@ * author: MagicDroidX * Nukkit Project */ -public enum LogLevel { +public enum LogLevel implements Comparable { + NONE, EMERGENCY, ALERT, CRITICAL, @@ -12,5 +13,10 @@ public enum LogLevel { WARNING, NOTICE, INFO, - DEBUG + DEBUG; + public static final LogLevel DEFAULT_LEVEL = INFO; + + int getLevel() { + return ordinal(); + } } diff --git a/src/main/java/cn/nukkit/utils/MainLogger.java b/src/main/java/cn/nukkit/utils/MainLogger.java index 0934f0afe8..0704e489e1 100644 --- a/src/main/java/cn/nukkit/utils/MainLogger.java +++ b/src/main/java/cn/nukkit/utils/MainLogger.java @@ -22,75 +22,84 @@ public class MainLogger extends ThreadedLogger { protected final String logPath; protected final ConcurrentLinkedQueue logBuffer = new ConcurrentLinkedQueue<>(); protected boolean shutdown; - protected boolean logDebug = false; + protected LogLevel logLevel = LogLevel.DEFAULT_LEVEL; private final Map replacements = new EnumMap<>(TextFormat.class); private final TextFormat[] colors = TextFormat.values(); protected static MainLogger logger; public MainLogger(String logFile) { - this(logFile, false); + this(logFile, LogLevel.DEFAULT_LEVEL); } - public MainLogger(String logFile, boolean logDebug) { + public MainLogger(String logFile, LogLevel logLevel) { + if (logger != null) { throw new RuntimeException("MainLogger has been already created"); } logger = this; this.logPath = logFile; - this.logDebug = logDebug; this.start(); } + public MainLogger(String logFile, boolean logDebug) { + this(logFile, logDebug ? LogLevel.DEBUG : LogLevel.INFO); + } + public static MainLogger getLogger() { return logger; } @Override public void emergency(String message) { - this.send(TextFormat.RED + "[EMERGENCY] " + message); + if (logLevel.getLevel() <= LogLevel.EMERGENCY.getLevel()) + this.send(TextFormat.RED + "[EMERGENCY] " + message); } @Override public void alert(String message) { - this.send(TextFormat.RED + "[ALERT] " + message); + if (logLevel.getLevel() <= LogLevel.ALERT.getLevel()) + this.send(TextFormat.RED + "[ALERT] " + message); } @Override public void critical(String message) { - this.send(TextFormat.RED + "[CRITICAL] " + message); + if (logLevel.getLevel() <= LogLevel.CRITICAL.getLevel()) + this.send(TextFormat.RED + "[CRITICAL] " + message); } @Override public void error(String message) { - this.send(TextFormat.DARK_RED + "[ERROR] " + message); + if (logLevel.getLevel() <= LogLevel.ERROR.getLevel()) + this.send(TextFormat.DARK_RED + "[ERROR] " + message); } @Override public void warning(String message) { - this.send(TextFormat.YELLOW + "[WARNING] " + message); + if (logLevel.getLevel() <= LogLevel.WARNING.getLevel()) + this.send(TextFormat.YELLOW + "[WARNING] " + message); } @Override public void notice(String message) { - this.send(TextFormat.AQUA + "[NOTICE] " + message); + if (logLevel.getLevel() <= LogLevel.NOTICE.getLevel()) + this.send(TextFormat.AQUA + "[NOTICE] " + message); } @Override public void info(String message) { - this.send(TextFormat.WHITE + "[INFO] " + message); + if (logLevel.getLevel() <= LogLevel.INFO.getLevel()) + this.send(TextFormat.WHITE + "[INFO] " + message); } @Override public void debug(String message) { - if (!this.logDebug) { - return; - } - this.send(TextFormat.GRAY + "[DEBUG] " + message); + if (logLevel.getLevel() <= LogLevel.DEBUG.getLevel()) + this.send(TextFormat.GRAY + "[DEBUG] " + message); } public void setLogDebug(Boolean logDebug) { - this.logDebug = logDebug; + this.logLevel = logDebug ? LogLevel.INFO : LogLevel.DEBUG; } public void logException(Exception e) {