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 | |
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')
6 files changed, 88 insertions, 79 deletions
diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java index 03585b7a..1b116878 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 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; } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 00000000..c868e8c5 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="WARN" packages="net.minecraft,com.mojang"> + <Appenders> + <Queue name="TerminalConsole"> + <PatternLayout pattern="[%d{HH:mm:ss} %level]: %msg%n" /> + </Queue> + <RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz"> + <PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n" /> + <Policies> + <TimeBasedTriggeringPolicy /> + <OnStartupTriggeringPolicy /> + </Policies> + </RollingRandomAccessFile> + </Appenders> + <Loggers> + <Root level="info"> + <filters> + <MarkerFilter marker="NETWORK_PACKETS" onMatch="DENY" onMismatch="NEUTRAL" /> + </filters> + <AppenderRef ref="File"/> + <AppenderRef ref="TerminalConsole"/> + </Root> + </Loggers> +</Configuration> + |