From 7f79bbbc9a128754585203594094ab05cffdf94f Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Thu, 27 Aug 2020 04:28:04 -0700 Subject: [PATCH] Refactored LogManager instance methods into LoggerRegistry LogManager was having method name conflicts LogManager delegates static methods to a LoggerRegistry instance --- core/src/com/riiablo/logger/LogManager.java | 98 ++----------------- .../com/riiablo/logger/LoggerRegistry.java | 97 ++++++++++++++++++ 2 files changed, 103 insertions(+), 92 deletions(-) create mode 100644 core/src/com/riiablo/logger/LoggerRegistry.java diff --git a/core/src/com/riiablo/logger/LogManager.java b/core/src/com/riiablo/logger/LogManager.java index 72020819..a20caf52 100644 --- a/core/src/com/riiablo/logger/LogManager.java +++ b/core/src/com/riiablo/logger/LogManager.java @@ -1,105 +1,19 @@ package com.riiablo.logger; -import java.util.SortedMap; -import org.apache.commons.collections4.Trie; -import org.apache.commons.collections4.trie.PatriciaTrie; -import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; public class LogManager { - private static final LogManager INSTANCE = new LogManager(); - private static final char PACKAGE_SEPARATOR = ClassUtils.PACKAGE_SEPARATOR_CHAR; + public static final LoggerRegistry INSTANCE = new LoggerRegistry(); public static final String ROOT = StringUtils.EMPTY; -// public static Logger getLogger(Class clazz) { -// return INSTANCE.get(clazz); -// } -// -// public static Logger getLogger(String name) { -// return INSTANCE.get(name); -// } - - LogManager() {} - - private final Trie loggers = new PatriciaTrie<>(); - private final Trie contexts = new PatriciaTrie<>(); - private final Logger ROOT_LOGGER = createRootLogger(); - - private Logger createRootLogger() { - final Logger root = new Logger(ROOT); - root.level(Level.WARN); - loggers.put(ROOT, root); - contexts.put(ROOT, root.level()); - return root; + public static Logger getLogger(Class clazz) { + return INSTANCE.getLogger(clazz); } - public Logger getRoot() { - return ROOT_LOGGER; + public static Logger getLogger(String name) { + return INSTANCE.getLogger(name); } - public Logger getLogger(Class clazz) { - return getLogger(clazz.getCanonicalName()); - } - - public Logger getLogger(String name) { - if (ROOT_LOGGER.appender() == null) { - throw new IllegalStateException("Cannot initialize logger without ROOT appender set!"); - } - - Logger logger = loggers.get(name); - if (logger == null) { - loggers.put(name, logger = new Logger(name)); - logger.addAppender(ROOT_LOGGER.appender()); - final Level context = resolve(logger.name()); - logger.bind(context, true); - } - - return logger; - } - - public Level getLevel(String name) { - return resolve(name); - } - - Level resolve(String name) { - Level context = contexts.get(name); - if (context != null) return context; - for (int i = name.length(); - (i = StringUtils.lastIndexOf(name, PACKAGE_SEPARATOR, i - 1)) != -1; ) { - context = contexts.get(name.substring(0, i)); - if (context != null) return context; - } - - assert contexts.get(ROOT) == ROOT_LOGGER.level(); - return ROOT_LOGGER.level(); - } - - public void setLevel(String name, Level level) { - setLevel(name, level, false); - } - - public void setLevel(String name, Level level, boolean force) { - bind(name, level, force); - bindAll(name, level, force); - } - - void bind(String name, Level context, boolean force) { - contexts.put(name, context); - final Logger logger = loggers.get(name); - if (logger != null) logger.bind(context, force); - } - - void bindAll(String pkg, Level context, boolean force) { - pkg = StringUtils.appendIfMissing(pkg, String.valueOf(PACKAGE_SEPARATOR)); - for (SortedMap.Entry entry - : contexts.prefixMap(pkg).entrySet()) { - entry.setValue(context); - } - - for (Logger logger - : loggers.prefixMap(pkg).values()) { - logger.bind(context, force); - } - } + private LogManager() {} } diff --git a/core/src/com/riiablo/logger/LoggerRegistry.java b/core/src/com/riiablo/logger/LoggerRegistry.java new file mode 100644 index 00000000..577e96c1 --- /dev/null +++ b/core/src/com/riiablo/logger/LoggerRegistry.java @@ -0,0 +1,97 @@ +package com.riiablo.logger; + +import java.util.SortedMap; +import org.apache.commons.collections4.Trie; +import org.apache.commons.collections4.trie.PatriciaTrie; +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; + +public class LoggerRegistry { + private static final char PACKAGE_SEPARATOR = ClassUtils.PACKAGE_SEPARATOR_CHAR; + + public static final String ROOT = LogManager.ROOT; + + LoggerRegistry() {} + + private final Trie loggers = new PatriciaTrie<>(); + private final Trie contexts = new PatriciaTrie<>(); + private final Logger ROOT_LOGGER = createRootLogger(); + + private Logger createRootLogger() { + final Logger root = new Logger(ROOT); + root.level(Level.WARN); + root.addAppender(new OutputStreamAppender(System.out)); + loggers.put(ROOT, root); + contexts.put(ROOT, root.level()); + return root; + } + + public Logger getRoot() { + return ROOT_LOGGER; + } + + public Logger getLogger(Class clazz) { + return getLogger(clazz.getCanonicalName()); + } + + public Logger getLogger(String name) { + if (ROOT_LOGGER.appender() == null) { + throw new IllegalStateException("Cannot initialize logger without ROOT appender set!"); + } + + Logger logger = loggers.get(name); + if (logger == null) { + loggers.put(name, logger = new Logger(name)); + logger.addAppender(ROOT_LOGGER.appender()); + final Level context = resolve(logger.name()); + logger.bind(context, true); + } + + return logger; + } + + public Level getLevel(String name) { + return resolve(name); + } + + Level resolve(String name) { + Level context = contexts.get(name); + if (context != null) return context; + for (int i = name.length(); + (i = StringUtils.lastIndexOf(name, PACKAGE_SEPARATOR, i - 1)) != -1; ) { + context = contexts.get(name.substring(0, i)); + if (context != null) return context; + } + + assert contexts.get(ROOT) == ROOT_LOGGER.level(); + return ROOT_LOGGER.level(); + } + + public void setLevel(String name, Level level) { + setLevel(name, level, false); + } + + public void setLevel(String name, Level level, boolean force) { + bind(name, level, force); + bindAll(name, level, force); + } + + void bind(String name, Level context, boolean force) { + contexts.put(name, context); + final Logger logger = loggers.get(name); + if (logger != null) logger.bind(context, force); + } + + void bindAll(String pkg, Level context, boolean force) { + pkg = StringUtils.appendIfMissing(pkg, String.valueOf(PACKAGE_SEPARATOR)); + for (SortedMap.Entry entry + : contexts.prefixMap(pkg).entrySet()) { + entry.setValue(context); + } + + for (Logger logger + : loggers.prefixMap(pkg).values()) { + logger.bind(context, force); + } + } +}