Allow to turn on debug mode for specified loggers

This commit is contained in:
yushijinhun 2018-07-08 15:21:46 +08:00
parent 177a647878
commit 45b4bfba0d
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
2 changed files with 35 additions and 15 deletions

View file

@ -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";

View file

@ -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<String> createDebugLoggerNamePredicate() {
String argument = System.getProperty(PROP_DEBUG);
if (argument == null) {
return any -> false;
} else {
Set<String> 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<String> namePredicate = createDebugLoggerNamePredicate();
return log -> log.getLevel().intValue() >= Level.INFO.intValue() || namePredicate.test(log.getLoggerName());
}
}