From 45b4bfba0deaeec80b8e653cfc0cdf641a3f3e2d Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sun, 8 Jul 2018 15:21:46 +0800 Subject: [PATCH] Allow to turn on debug mode for specified loggers --- .../authlibinjector/AuthlibInjector.java | 2 +- .../yushi/authlibinjector/util/Logging.java | 48 +++++++++++++------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java index b50230f..4841f2c 100644 --- a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java +++ b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java @@ -51,7 +51,7 @@ public final class AuthlibInjector { public static final String PROP_DISABLE_HTTPD = "authlibinjector.httpd.disable"; /** - * Whether to turn on debug logging. + * The name of loggers to have debug level turned on. */ public static final String PROP_DEBUG = "authlibinjector.debug"; diff --git a/src/main/java/moe/yushi/authlibinjector/util/Logging.java b/src/main/java/moe/yushi/authlibinjector/util/Logging.java index a0fd8bf..81e832a 100644 --- a/src/main/java/moe/yushi/authlibinjector/util/Logging.java +++ b/src/main/java/moe/yushi/authlibinjector/util/Logging.java @@ -4,6 +4,10 @@ import static moe.yushi.authlibinjector.AuthlibInjector.PROP_DEBUG; import java.io.PrintWriter; import java.io.StringWriter; import java.text.MessageFormat; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Predicate; +import java.util.logging.Filter; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -13,25 +17,25 @@ import java.util.logging.StreamHandler; public final class Logging { private Logging() {} - public static final Logger ROOT = Logger.getLogger("moe.yushi.authlibinjector"); - public static final Logger LAUNCH = Logger.getLogger("moe.yushi.authlibinjector.launch"); - public static final Logger CONFIG = Logger.getLogger("moe.yushi.authlibinjector.config"); - public static final Logger TRANSFORM = Logger.getLogger("moe.yushi.authlibinjector.transform"); - public static final Logger HTTPD = Logger.getLogger("moe.yushi.authlibinjector.httpd"); + private static final String PREFIX = "moe.yushi.authlibinjector"; + + public static final Logger ROOT = Logger.getLogger(PREFIX); + public static final Logger LAUNCH = Logger.getLogger(PREFIX + ".launch"); + public static final Logger CONFIG = Logger.getLogger(PREFIX + ".config"); + public static final Logger TRANSFORM = Logger.getLogger(PREFIX + ".transform"); + public static final Logger HTTPD = Logger.getLogger(PREFIX + ".httpd"); public static void init() { initRootLogger(); - initLoggingLevels(); } private static void initRootLogger() { - ROOT.setLevel(Level.INFO); + ROOT.setLevel(Level.ALL); ROOT.setUseParentHandlers(false); StreamHandler handler = new StreamHandler(System.err, new Formatter() { private String convertLoggerName(String loggerName) { - final String prefix = "moe.yushi.authlibinjector"; - if (loggerName.startsWith(prefix)) { - return "authlib-injector" + loggerName.substring(prefix.length()); + if (loggerName.startsWith(PREFIX)) { + return "authlib-injector" + loggerName.substring(PREFIX.length()); } else { return loggerName; } @@ -63,14 +67,30 @@ public final class Logging { } }; handler.setLevel(Level.ALL); - handler.setFilter(null); + handler.setFilter(createFilter()); ROOT.addHandler(handler); } - private static void initLoggingLevels() { - if ("true".equals(System.getProperty(PROP_DEBUG))) { - ROOT.setLevel(Level.ALL); + private static Predicate createDebugLoggerNamePredicate() { + String argument = System.getProperty(PROP_DEBUG); + if (argument == null) { + return any -> false; + } else { + Set debugLoggers = new HashSet<>(); + for (String element : argument.split(",")) { + if (element.equals("true") || element.equals("all")) { + return loggerName -> loggerName.startsWith(PREFIX); + } else { + debugLoggers.add(PREFIX + "." + element); + } + } + return debugLoggers::contains; } } + private static Filter createFilter() { + Predicate namePredicate = createDebugLoggerNamePredicate(); + return log -> log.getLevel().intValue() >= Level.INFO.intValue() || namePredicate.test(log.getLoggerName()); + } + }