Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-12-18 12:30:06 +01:00
Forward exceptions to log4j, cleanup logger handling. Fixes BUKKIT-4948
Dieser Commit ist enthalten in:
Ursprung
2726696652
Commit
c098854591
@ -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");
|
||||
|
@ -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();
|
||||
|
52
src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java
Normale Datei
52
src/main/java/org/bukkit/craftbukkit/util/ForwardLogHandler.java
Normale Datei
@ -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 {
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
25
src/main/resources/log4j2.xml
Normale Datei
25
src/main/resources/log4j2.xml
Normale Datei
@ -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>
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren