From c098854591a90a2c8d384663a00cb7cb73cef0ac Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sun, 1 Dec 2013 11:15:18 -0600 Subject: [PATCH] Forward exceptions to log4j, cleanup logger handling. Fixes BUKKIT-4948 --- .../net/minecraft/server/DedicatedServer.java | 17 ++--- .../org/bukkit/craftbukkit/CraftServer.java | 3 +- .../craftbukkit/util/ForwardLogHandler.java | 52 ++++++++++++++ .../craftbukkit/util/Log4jConverter.java | 68 ------------------- .../util/TerminalConsoleWriterThread.java | 2 +- src/main/resources/log4j2.xml | 25 +++++++ 6 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java delete mode 100644 src/main/java/org/bukkit/craftbukkit/util/Log4jConverter.java create mode 100644 src/main/resources/log4j2.xml diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java index 03585b7ac5..1b11687877 100644 --- a/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/DedicatedServer.java @@ -46,18 +46,19 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer threadcommandreader.setDaemon(true); threadcommandreader.start(); - // CraftBukkit start - TODO: See if this needs fixing - final org.apache.logging.log4j.core.Logger realLogger = ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()); - for (org.apache.logging.log4j.core.Appender appender : realLogger.getAppenders().values()) { - if (appender instanceof org.apache.logging.log4j.core.appender.ConsoleAppender) { - realLogger.removeAppender(appender); - } + // CraftBukkit start - TODO: handle command-line logging arguments + java.util.logging.Logger global = java.util.logging.Logger.getLogger(""); + global.setUseParentHandlers(false); + for (java.util.logging.Handler handler : global.getHandlers()) { + global.removeHandler(handler); } + global.addHandler(new org.bukkit.craftbukkit.util.ForwardLogHandler()); + final org.apache.logging.log4j.core.Logger logger = ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()); new Thread(new org.bukkit.craftbukkit.util.TerminalConsoleWriterThread(System.out, this.reader)).start(); - System.setOut(new PrintStream(new LoggerOutputStream(realLogger, Level.INFO), true)); - System.setErr(new PrintStream(new LoggerOutputStream(realLogger, Level.WARN), true)); + System.setOut(new PrintStream(new LoggerOutputStream(logger, Level.INFO), true)); + System.setErr(new PrintStream(new LoggerOutputStream(logger, Level.WARN), true)); // CraftBukkit end h.info("Starting minecraft server version 1.7.2"); diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index caa184ee6e..362b507b9d 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 0000000000..124c8ce53d --- /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 cachedLoggers = new ConcurrentHashMap(); + + 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 fbaf51a750..0000000000 --- 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 JULTOLOG4J = new HashMap(); - private static final Map LOG4JTOJUL = new EnumMap(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 e8ace74137..0bdfde6d55 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; } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..c868e8c5a9 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + +