Waterfall/BungeeCord-Patches/0037-Use-Log4j2-for-logging-and-TerminalConsoleAppender-f.patch
Shane Freeder 8f8bdf58a9 Updated Upstream (BungeeCord)
Upstream has released updates that appears to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

BungeeCord Changes:
771f1735 Clearer message for illegal IP addresses
4428409d Netty 4.1.34.Final
52a125dd Add --help flag
2019-03-20 03:55:00 +00:00

462 lines
17 KiB
Diff

From b4677c171d3d0aae5a492540823c2b737db5fa41 Mon Sep 17 00:00:00 2001
From: Minecrell <minecrell@minecrell.net>
Date: Fri, 22 Sep 2017 12:46:47 +0200
Subject: [PATCH] Use Log4j2 for logging and TerminalConsoleAppender for
console
diff --git a/bootstrap/pom.xml b/bootstrap/pom.xml
index ba94c757..6cacf112 100644
--- a/bootstrap/pom.xml
+++ b/bootstrap/pom.xml
@@ -47,6 +47,9 @@
<Main-Class>net.md_5.bungee.Bootstrap</Main-Class>
<Implementation-Version>${describe}</Implementation-Version>
<Specification-Version>${maven.build.timestamp}</Specification-Version>
+
+ <!-- Log4j includes custom classes for Java 9+ (#293) -->
+ <Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
@@ -74,7 +77,17 @@
</excludes>
</filter>
</filters>
+ <transformers>
+ <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />
+ </transformers>
</configuration>
+ <dependencies>
+ <dependency>
+ <groupId>com.github.edwgiz</groupId>
+ <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
+ <version>2.8.1</version>
+ </dependency>
+ </dependencies>
</plugin>
</plugins>
</build>
diff --git a/log4j/pom.xml b/log4j/pom.xml
new file mode 100644
index 00000000..e27b582f
--- /dev/null
+++ b/log4j/pom.xml
@@ -0,0 +1,48 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>io.github.waterfallmc</groupId>
+ <artifactId>waterfall-parent</artifactId>
+ <version>1.13-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <groupId>io.github.waterfallmc</groupId>
+ <artifactId>waterfall-log4j</artifactId>
+ <version>1.13-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>Waterfall-Log</name>
+ <description>Simplistic and performant Log4j2 based logger and console API designed for use with Waterfall and Minecraft related applications.</description>
+
+ <properties>
+ <log4j2.version>2.11.1</log4j2.version>
+ </properties>
+
+ <dependencies>
+ <!-- Log4j -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>${log4j2.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-iostreams</artifactId>
+ <version>${log4j2.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-jul</artifactId>
+ <version>${log4j2.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.lmax</groupId>
+ <artifactId>disruptor</artifactId>
+ <version>3.4.2</version>
+ <scope>runtime</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/Log4JLogHandler.java b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/Log4JLogHandler.java
new file mode 100644
index 00000000..63f66d3c
--- /dev/null
+++ b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/Log4JLogHandler.java
@@ -0,0 +1,59 @@
+package io.github.waterfallmc.waterfall.log4j;
+
+import com.google.common.base.Strings;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.jul.LevelTranslator;
+import org.apache.logging.log4j.message.MessageFormatMessage;
+
+import java.util.Map;
+import java.util.MissingResourceException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+
+/**
+ * A {@link Handler} that forwards all log messages to the Log4J logger.
+ *
+ * <p>We don't use Log4J's custom JUL LogManager currently, because it breaks
+ * adding custom handlers to JUL loggers. Some plugins may depend on that
+ * functionality...</p>
+ */
+class Log4JLogHandler extends Handler {
+
+ private final Map<String, Logger> cache = new ConcurrentHashMap<>();
+
+ @Override
+ public void publish(LogRecord record) {
+ if (!isLoggable(record)) {
+ return;
+ }
+
+ Logger logger = cache.computeIfAbsent(Strings.nullToEmpty(record.getLoggerName()), LogManager::getLogger);
+
+ String message = record.getMessage();
+ if (record.getResourceBundle() != null) {
+ try {
+ message = record.getResourceBundle().getString(message);
+ } catch (MissingResourceException ignored) {
+ }
+ }
+
+ final Level level = LevelTranslator.toLevel(record.getLevel());
+ if (record.getParameters() != null && record.getParameters().length > 0) {
+ logger.log(level, new MessageFormatMessage(message, record.getParameters()), record.getThrown());
+ } else {
+ logger.log(level, message, record.getThrown());
+ }
+ }
+
+ @Override
+ public void flush() {
+ }
+
+ @Override
+ public void close() {
+ }
+
+}
diff --git a/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/WaterfallLogger.java b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/WaterfallLogger.java
new file mode 100644
index 00000000..e046897a
--- /dev/null
+++ b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/WaterfallLogger.java
@@ -0,0 +1,34 @@
+package io.github.waterfallmc.waterfall.log4j;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.io.IoBuilder;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+
+public final class WaterfallLogger {
+
+ private WaterfallLogger() {
+ }
+
+ public static Logger create() {
+ org.apache.logging.log4j.Logger redirect = LogManager.getRootLogger();
+ System.setOut(IoBuilder.forLogger(redirect).setLevel(Level.INFO).buildPrintStream());
+ System.setErr(IoBuilder.forLogger(redirect).setLevel(Level.ERROR).buildPrintStream());
+
+ Logger root = Logger.getLogger("");
+ root.setUseParentHandlers(false);
+
+ // Remove existing handlers
+ for (Handler handler : root.getHandlers()) {
+ root.removeHandler(handler);
+ }
+
+ // Setup forward log handler
+ root.setLevel(java.util.logging.Level.ALL); // Log4j will handle filtering the log
+ root.addHandler(new Log4JLogHandler());
+
+ return Logger.getLogger("BungeeCord");
+ }
+
+}
diff --git a/log4j/src/main/resources/log4j2.component.properties b/log4j/src/main/resources/log4j2.component.properties
new file mode 100644
index 00000000..6ed08f31
--- /dev/null
+++ b/log4j/src/main/resources/log4j2.component.properties
@@ -0,0 +1 @@
+log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
diff --git a/log4j/src/main/resources/log4j2.xml b/log4j/src/main/resources/log4j2.xml
new file mode 100644
index 00000000..93ce3b14
--- /dev/null
+++ b/log4j/src/main/resources/log4j2.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="warn">
+ <Appenders>
+ <TerminalConsole name="TerminalConsole">
+ <PatternLayout pattern="%highlightError{[%d{HH:mm:ss} %level]: %minecraftFormatting{%msg}%n%xEx}" />
+ </TerminalConsole>
+ <RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz" immediateFlush="false">
+ <PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %minecraftFormatting{%msg}{strip}%n" />
+ <Policies>
+ <TimeBasedTriggeringPolicy />
+ <OnStartupTriggeringPolicy />
+ </Policies>
+ </RollingRandomAccessFile>
+ </Appenders>
+
+ <Loggers>
+ <Root level="info">
+ <AppenderRef ref="TerminalConsole" />
+ <AppenderRef ref="File" />
+ </Root>
+ </Loggers>
+</Configuration>
diff --git a/pom.xml b/pom.xml
index daaf18b9..4a007355 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,7 @@
<module>config</module>
<module>event</module>
<module>log</module>
+ <module>log4j</module>
<module>module</module>
<module>protocol</module>
<module>proxy</module>
diff --git a/proxy/pom.xml b/proxy/pom.xml
index fa1e6046..f0497f59 100644
--- a/proxy/pom.xml
+++ b/proxy/pom.xml
@@ -70,7 +70,7 @@
</dependency>
<dependency>
<groupId>io.github.waterfallmc</groupId>
- <artifactId>waterfall-log</artifactId>
+ <artifactId>waterfall-log4j</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
@@ -104,6 +104,20 @@
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
+
+ <!-- Waterfall start - Console improvements -->
+ <dependency>
+ <groupId>net.minecrell</groupId>
+ <artifactId>terminalconsoleappender</artifactId>
+ <version>1.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jna</groupId>
+ <artifactId>jna</artifactId>
+ <version>4.5.2</version>
+ <scope>runtime</scope>
+ </dependency>
+ <!-- Waterfall end -->
</dependencies>
<build>
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
new file mode 100644
index 00000000..765d24bc
--- /dev/null
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
@@ -0,0 +1,39 @@
+package io.github.waterfallmc.waterfall.console;
+
+import net.md_5.bungee.BungeeCord;
+import net.md_5.bungee.api.ChatColor;
+import net.md_5.bungee.api.ProxyServer;
+import net.md_5.bungee.api.chat.ComponentBuilder;
+import net.minecrell.terminalconsole.SimpleTerminalConsole;
+import org.jline.reader.LineReader;
+import org.jline.reader.LineReaderBuilder;
+
+public final class WaterfallConsole extends SimpleTerminalConsole {
+
+ @Override
+ protected LineReader buildReader(LineReaderBuilder builder) {
+ ProxyServer proxy = ProxyServer.getInstance();
+ return super.buildReader(builder
+ .appName(proxy.getName())
+ );
+ }
+
+ @Override
+ protected boolean isRunning() {
+ return BungeeCord.getInstance().isRunning;
+ }
+
+ @Override
+ protected void runCommand(String command) {
+ ProxyServer proxy = ProxyServer.getInstance();
+ if (!proxy.getPluginManager().dispatchCommand(proxy.getConsole(), command)) {
+ proxy.getConsole().sendMessage(new ComponentBuilder("Command not found").color(ChatColor.RED).create());
+ }
+ }
+
+ @Override
+ protected void shutdown() {
+ ProxyServer.getInstance().stop();
+ }
+
+}
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 00f8a19c..c407cc97 100644
--- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
+++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
@@ -44,7 +44,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
-import jline.console.ConsoleReader;
import lombok.Getter;
import lombok.Setter;
import lombok.Synchronized;
@@ -77,14 +76,11 @@ import net.md_5.bungee.command.CommandEnd;
import net.md_5.bungee.command.CommandIP;
import net.md_5.bungee.command.CommandPerms;
import net.md_5.bungee.command.CommandReload;
-import net.md_5.bungee.command.ConsoleCommandCompleter;
import net.md_5.bungee.command.ConsoleCommandSender;
import net.md_5.bungee.compress.CompressFactory;
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.LoggingOutputStream;
import net.md_5.bungee.module.ModuleManager;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.DefinedPacket;
@@ -94,7 +90,6 @@ import net.md_5.bungee.protocol.packet.PluginMessage;
import net.md_5.bungee.query.RemoteQuery;
import net.md_5.bungee.scheduler.BungeeScheduler;
import net.md_5.bungee.util.CaseInsensitiveMap;
-import org.fusesource.jansi.AnsiConsole;
/**
* Main BungeeCord proxy class.
@@ -150,8 +145,12 @@ public class BungeeCord extends ProxyServer
private final File pluginsFolder = new File( "plugins" );
@Getter
private final BungeeScheduler scheduler = new BungeeScheduler();
+ // Waterfall start - Remove ConsoleReader for JLine 3 update
+ /*
@Getter
private final ConsoleReader consoleReader;
+ */
+ // Waterfall end
@Getter
private final Logger logger;
public final Gson gson = new GsonBuilder()
@@ -193,6 +192,8 @@ public class BungeeCord extends ProxyServer
}
reloadMessages();
+ // Waterfall start - Use TerminalConsoleAppender and Log4J
+ /*
// This is a workaround for quite possibly the weirdest bug I have ever encountered in my life!
// When jansi attempts to extract its natives, by default it tries to extract a specific version,
// using the loading class's implementation version. Normally this works completely fine,
@@ -211,6 +212,9 @@ public class BungeeCord extends ProxyServer
logger = new BungeeLogger( "BungeeCord", "proxy.log", consoleReader );
System.setErr( new PrintStream( new LoggingOutputStream( logger, Level.SEVERE ), true ) );
System.setOut( new PrintStream( new LoggingOutputStream( logger, Level.INFO ), true ) );
+ */
+ logger = io.github.waterfallmc.waterfall.log4j.WaterfallLogger.create();
+ // Waterfall end
pluginManager = new PluginManager( this );
getPluginManager().registerCommand( null, new CommandReload() );
diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java b/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
index 9fab61ec..624eb5f0 100644
--- a/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
+++ b/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
@@ -63,6 +63,9 @@ public class BungeeCordLauncher
if ( !options.has( "noconsole" ) )
{
+ // Waterfall start - Use TerminalConsoleAppender
+ new io.github.waterfallmc.waterfall.console.WaterfallConsole().start();
+ /*
String line;
while ( bungee.isRunning && ( line = bungee.getConsoleReader().readLine( ">" ) ) != null )
{
@@ -71,6 +74,8 @@ public class BungeeCordLauncher
bungee.getConsole().sendMessage( new ComponentBuilder( "Command not found" ).color( ChatColor.RED ).create() );
}
}
+ */
+ // Waterfall end
}
}
}
diff --git a/proxy/src/main/java/net/md_5/bungee/command/ConsoleCommandCompleter.java b/proxy/src/main/java/net/md_5/bungee/command/ConsoleCommandCompleter.java
deleted file mode 100644
index bd52d880..00000000
--- a/proxy/src/main/java/net/md_5/bungee/command/ConsoleCommandCompleter.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package net.md_5.bungee.command;
-
-import java.util.ArrayList;
-import java.util.List;
-import jline.console.completer.Completer;
-import lombok.RequiredArgsConstructor;
-import net.md_5.bungee.api.ProxyServer;
-
-@RequiredArgsConstructor
-public class ConsoleCommandCompleter implements Completer
-{
-
- private final ProxyServer proxy;
-
- @Override
- public int complete(String buffer, int cursor, List<CharSequence> candidates)
- {
- List<String> suggestions = new ArrayList<>();
- proxy.getPluginManager().dispatchCommand( proxy.getConsole(), buffer, suggestions );
- candidates.addAll( suggestions );
-
- int lastSpace = buffer.lastIndexOf( ' ' );
- return ( lastSpace == -1 ) ? cursor - buffer.length() : cursor - ( buffer.length() - lastSpace - 1 );
- }
-}
diff --git a/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java b/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java
index 10366c88..8c5cc949 100644
--- a/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java
+++ b/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java
@@ -42,7 +42,7 @@ public class ModuleManager
ModuleVersion bungeeVersion = ModuleVersion.parse( proxy.getVersion() );
if ( bungeeVersion == null )
{
- System.out.println( "Couldn't detect bungee version. Custom build?" );
+ proxy.getLogger().warning( "Couldn't detect bungee version. Custom build?" ); // Waterfall - Use logger
return;
}
--
2.21.0