From c3e8cfac793a3572c8e70fb1fab5aaf4fa57d680 Mon Sep 17 00:00:00 2001 From: floge07 <53813973+floge07@users.noreply.github.com> Date: Sat, 12 Nov 2022 11:51:14 +1100 Subject: [PATCH] #3374, #3389: Improve log handling of normal java.util Logger usage by forwarding the LogRecords directly to the BungeeLogger instead of the fallback err stream. --- .../net/md_5/bungee/log/BungeeLogger.java | 1 + .../bungee/log/LoggingForwardHandler.java | 29 +++++++++++++++++++ .../main/java/net/md_5/bungee/BungeeCord.java | 16 ++++++++++ 3 files changed, 46 insertions(+) create mode 100644 log/src/main/java/net/md_5/bungee/log/LoggingForwardHandler.java diff --git a/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java b/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java index 8e2f1bf3..d7d2b6a9 100644 --- a/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java +++ b/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java @@ -24,6 +24,7 @@ public class BungeeLogger extends Logger { super( loggerName, null ); setLevel( Level.ALL ); + setUseParentHandlers( false ); try { diff --git a/log/src/main/java/net/md_5/bungee/log/LoggingForwardHandler.java b/log/src/main/java/net/md_5/bungee/log/LoggingForwardHandler.java new file mode 100644 index 00000000..6974a7b3 --- /dev/null +++ b/log/src/main/java/net/md_5/bungee/log/LoggingForwardHandler.java @@ -0,0 +1,29 @@ +package net.md_5.bungee.log; + +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class LoggingForwardHandler extends Handler +{ + + private final Logger logger; + + @Override + public void publish(LogRecord record) + { + logger.log( record ); + } + + @Override + public void flush() + { + } + + @Override + public void close() throws SecurityException + { + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 5c3874f7..c2fe9a40 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -87,6 +87,7 @@ import net.md_5.bungee.conf.Configuration; import net.md_5.bungee.conf.YamlConfig; import net.md_5.bungee.forge.ForgeConstants; import net.md_5.bungee.log.BungeeLogger; +import net.md_5.bungee.log.LoggingForwardHandler; import net.md_5.bungee.log.LoggingOutputStream; import net.md_5.bungee.module.ModuleManager; import net.md_5.bungee.netty.PipelineUtils; @@ -209,6 +210,21 @@ public class BungeeCord extends ProxyServer logger = new BungeeLogger( "BungeeCord", "proxy.log", consoleReader ); JDK14LoggerFactory.LOGGER = logger; + + // Before we can set the Err and Out streams to our LoggingOutputStream we also have to remove + // the default ConsoleHandler from the root logger, which writes to the err stream. + // But we still want to log these records, so we add our own handler which forwards the LogRecord to the BungeeLogger. + // This way we skip the err stream and the problem of only getting a string without context, and can handle the LogRecord itself. + // Thus improving the default bahavior for projects that log on other Logger instances not created by BungeeCord. + Logger rootLogger = Logger.getLogger( "" ); + for ( Handler handler : rootLogger.getHandlers() ) + { + rootLogger.removeHandler( handler ); + } + rootLogger.addHandler( new LoggingForwardHandler( logger ) ); + + // We want everything that reaches these output streams to be handled by our logger + // since it applies a nice looking format and also writes to the logfile. System.setErr( new PrintStream( new LoggingOutputStream( logger, Level.SEVERE ), true ) ); System.setOut( new PrintStream( new LoggingOutputStream( logger, Level.INFO ), true ) );