diff options
author | Travis Watkins <amaranth@ubuntu.com> | 2013-12-01 11:15:18 -0600 |
---|---|---|
committer | Travis Watkins <amaranth@ubuntu.com> | 2013-12-01 11:15:18 -0600 |
commit | c098854591a90a2c8d384663a00cb7cb73cef0ac (patch) | |
tree | 3bfc3b7cc70608685783312475b296b080860872 /src/main/java/org | |
parent | 2726696652179a5ead7dc07dd39edf7dfda687f7 (diff) | |
download | craftbukkit-c098854591a90a2c8d384663a00cb7cb73cef0ac.tar craftbukkit-c098854591a90a2c8d384663a00cb7cb73cef0ac.tar.gz craftbukkit-c098854591a90a2c8d384663a00cb7cb73cef0ac.tar.lz craftbukkit-c098854591a90a2c8d384663a00cb7cb73cef0ac.tar.xz craftbukkit-c098854591a90a2c8d384663a00cb7cb73cef0ac.zip |
Forward exceptions to log4j, cleanup logger handling. Fixes BUKKIT-4948
Diffstat (limited to 'src/main/java/org')
4 files changed, 54 insertions, 71 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index caa184ee..362b507b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -94,7 +94,6 @@ import org.bukkit.craftbukkit.updater.AutoUpdater; import org.bukkit.craftbukkit.updater.BukkitDLUpdaterService; import org.bukkit.craftbukkit.util.CraftIconCache; import org.bukkit.craftbukkit.util.DatFileFilter; -import org.bukkit.craftbukkit.util.Log4jConverter; import org.bukkit.craftbukkit.util.Versioning; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; @@ -188,7 +187,7 @@ public final class CraftServer implements Server { } public CraftServer(MinecraftServer console, PlayerList playerList) { - this.logger = Log4jConverter.createLogger(); + this.logger = Logger.getLogger("Minecraft"); this.console = console; this.playerList = (DedicatedPlayerList) playerList; this.serverVersion = CraftServer.class.getPackage().getImplementationVersion(); diff --git a/src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java b/src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java new file mode 100644 index 00000000..124c8ce5 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java @@ -0,0 +1,52 @@ +package org.bukkit.craftbukkit.util; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +public class ForwardLogHandler extends Handler { + private Map<String, Logger> cachedLoggers = new ConcurrentHashMap<String, Logger>(); + + private Logger getLogger(String name) { + Logger logger = cachedLoggers.get(name); + if (logger == null) { + logger = LogManager.getLogger(name); + cachedLoggers.put(name, logger); + } + + return logger; + } + + @Override + public void publish(LogRecord record) { + Logger logger = getLogger(record.getLoggerName()); + String message = record.getMessage(); + Throwable exception = record.getThrown(); + Level level = record.getLevel(); + + if (level == Level.SEVERE) { + logger.error(message, exception); + } else if (level == Level.WARNING) { + logger.warn(message, exception); + } else if (level == Level.INFO) { + logger.info(message, exception); + } else if (level == Level.CONFIG) { + logger.debug(message, exception); + } else { + logger.trace(message, exception); + } + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/Log4jConverter.java b/src/main/java/org/bukkit/craftbukkit/util/Log4jConverter.java deleted file mode 100644 index fbaf51a7..00000000 --- a/src/main/java/org/bukkit/craftbukkit/util/Log4jConverter.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.bukkit.craftbukkit.util; - -import java.util.EnumMap; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; - -public class Log4jConverter { - - private static final Map<Level, org.apache.logging.log4j.Level> JULTOLOG4J = new HashMap<Level, org.apache.logging.log4j.Level>(); - private static final Map<org.apache.logging.log4j.Level, Level> LOG4JTOJUL = new EnumMap<org.apache.logging.log4j.Level, Level>(org.apache.logging.log4j.Level.class); - static { - JULTOLOG4J.put(Level.ALL, org.apache.logging.log4j.Level.ALL); - JULTOLOG4J.put(Level.FINEST, org.apache.logging.log4j.Level.TRACE); - JULTOLOG4J.put(Level.FINER, org.apache.logging.log4j.Level.TRACE); - JULTOLOG4J.put(Level.FINE, org.apache.logging.log4j.Level.TRACE); - JULTOLOG4J.put(Level.INFO, org.apache.logging.log4j.Level.INFO); - JULTOLOG4J.put(Level.CONFIG, org.apache.logging.log4j.Level.INFO); - JULTOLOG4J.put(Level.WARNING, org.apache.logging.log4j.Level.WARN); - JULTOLOG4J.put(Level.SEVERE, org.apache.logging.log4j.Level.ERROR); - JULTOLOG4J.put(Level.OFF, org.apache.logging.log4j.Level.OFF); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.ALL, Level.ALL); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.TRACE, Level.FINE); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.INFO, Level.INFO); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.DEBUG, Level.INFO); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.WARN, Level.WARNING); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.ERROR, Level.SEVERE); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.FATAL, Level.SEVERE); - LOG4JTOJUL.put(org.apache.logging.log4j.Level.OFF, Level.OFF); - } - - public static org.apache.logging.log4j.Level getLog4jLevel(Level level) { - org.apache.logging.log4j.Level log4jLevel = JULTOLOG4J.get(level); - return log4jLevel == null ? org.apache.logging.log4j.Level.INFO : log4jLevel; - } - - public static Level getJULLevel(org.apache.logging.log4j.Level level) { - return LOG4JTOJUL.get(level); - } - - public static Logger createLogger() { - final Logger logger = Logger.getLogger("Minecraft"); - // Grab a logger. It doesn't really matter which, so long as it is within net.minecraft.server. - final org.apache.logging.log4j.core.Logger log4j = (org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager.getLogger("net.minecraft.server.DedicatedServer"); - logger.setUseParentHandlers(false); - // Add a handler to our Bukkit Logger so that messages logged to it are sent to log4j. - Handler log4jHandler = new Handler() { - @Override - public void close() throws SecurityException { - } - - @Override - public void flush() { - } - - @Override - public void publish(LogRecord record) { - log4j.log(Log4jConverter.getLog4jLevel(record.getLevel()), record.getMessage()); - - } - }; - logger.addHandler(log4jHandler); - return logger; - } -} diff --git a/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java b/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java index e8ace741..0bdfde6d 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java +++ b/src/main/java/org/bukkit/craftbukkit/util/TerminalConsoleWriterThread.java @@ -22,7 +22,7 @@ public class TerminalConsoleWriterThread implements Runnable { // Using name from log4j config in vanilla jar while (true) { - message = QueueLogAppender.getNextLogEvent("ServerGuiConsole"); + message = QueueLogAppender.getNextLogEvent("TerminalConsole"); if (message == null) { continue; } |