2021-04-12 08:37:52 +02:00
From 92b6a65c214965a5dd2422d1d4e78eeffb793d6f Mon Sep 17 00:00:00 2001
2018-07-06 23:00:08 +02:00
From: Minecrell <minecrell@minecrell.net>
2017-09-22 12:47:50 +02:00
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
2021-02-28 20:14:43 +01:00
index 1bea329d..37c0880c 100644
2017-09-22 12:47:50 +02:00
--- a/bootstrap/pom.xml
+++ b/bootstrap/pom.xml
2020-02-10 23:39:19 +01:00
@@ -49,6 +49,9 @@
2018-10-15 19:43:36 +02:00
<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>
2020-02-10 23:39:19 +01:00
@@ -76,7 +79,17 @@
2017-09-22 12:47:50 +02:00
</excludes>
</filter>
</filters>
+ <transformers>
2020-10-23 04:01:15 +02:00
+ <transformer implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer" />
2017-09-22 12:47:50 +02:00
+ </transformers>
</configuration>
+ <dependencies>
+ <dependency>
+ <groupId>com.github.edwgiz</groupId>
+ <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
2020-10-23 04:01:15 +02:00
+ <version>2.13.2</version>
2017-09-22 12:47:50 +02:00
+ </dependency>
+ </dependencies>
</plugin>
</plugins>
</build>
diff --git a/log4j/pom.xml b/log4j/pom.xml
new file mode 100644
2021-02-28 20:14:43 +01:00
index 00000000..84039f14
2017-09-22 12:47:50 +02:00
--- /dev/null
+++ b/log4j/pom.xml
2018-07-06 23:00:08 +02:00
@@ -0,0 +1,48 @@
2017-09-22 12:47:50 +02:00
+<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>
2021-01-29 11:35:23 +01:00
+ <version>1.16-R0.5-SNAPSHOT</version>
2017-09-22 12:47:50 +02:00
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <groupId>io.github.waterfallmc</groupId>
+ <artifactId>waterfall-log4j</artifactId>
2021-01-29 11:35:23 +01:00
+ <version>1.16-R0.5-SNAPSHOT</version>
2017-09-22 12:47:50 +02:00
+ <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>
+
2018-07-06 23:00:08 +02:00
+ <properties>
2021-02-28 20:14:43 +01:00
+ <log4j2.version>2.14.0</log4j2.version>
2018-07-06 23:00:08 +02:00
+ </properties>
+
2017-09-22 12:47:50 +02:00
+ <dependencies>
+ <!-- Log4j -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
2018-07-06 23:00:08 +02:00
+ <version>${log4j2.version}</version>
2017-09-22 12:47:50 +02:00
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-iostreams</artifactId>
2018-07-06 23:00:08 +02:00
+ <version>${log4j2.version}</version>
2017-09-22 12:47:50 +02:00
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-jul</artifactId>
2018-07-06 23:00:08 +02:00
+ <version>${log4j2.version}</version>
2017-09-22 12:47:50 +02:00
+ </dependency>
+ <dependency>
+ <groupId>com.lmax</groupId>
+ <artifactId>disruptor</artifactId>
2018-07-06 23:00:08 +02:00
+ <version>3.4.2</version>
2017-09-22 12:47:50 +02:00
+ <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
2018-08-21 21:26:07 +02:00
index 00000000..63f66d3c
2017-09-22 12:47:50 +02:00
--- /dev/null
+++ b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/Log4JLogHandler.java
2018-08-21 21:26:07 +02:00
@@ -0,0 +1,59 @@
2017-09-22 12:47:50 +02:00
+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) {
2018-08-21 21:26:07 +02:00
+ if (!isLoggable(record)) {
+ return;
+ }
+
2017-09-22 12:47:50 +02:00
+ 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
2018-08-21 21:26:07 +02:00
index 00000000..e046897a
2017-09-22 12:47:50 +02:00
--- /dev/null
+++ b/log4j/src/main/java/io/github/waterfallmc/waterfall/log4j/WaterfallLogger.java
2018-08-21 21:26:07 +02:00
@@ -0,0 +1,34 @@
2017-09-22 12:47:50 +02:00
+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
2018-08-21 21:26:07 +02:00
+ root.setLevel(java.util.logging.Level.ALL); // Log4j will handle filtering the log
2017-09-22 12:47:50 +02:00
+ root.addHandler(new Log4JLogHandler());
+
+ return Logger.getLogger("BungeeCord");
+ }
+
+}
2018-07-18 16:43:25 +02:00
diff --git a/log4j/src/main/resources/log4j2.component.properties b/log4j/src/main/resources/log4j2.component.properties
new file mode 100644
2019-07-21 16:25:47 +02:00
index 00000000..e43f5dac
2018-07-18 16:43:25 +02:00
--- /dev/null
+++ b/log4j/src/main/resources/log4j2.component.properties
2019-07-21 16:25:47 +02:00
@@ -0,0 +1,2 @@
2018-07-18 16:43:25 +02:00
+log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
2019-07-21 16:25:47 +02:00
+log4j.skipJansi=true
2017-09-22 12:47:50 +02:00
diff --git a/log4j/src/main/resources/log4j2.xml b/log4j/src/main/resources/log4j2.xml
new file mode 100644
2018-07-18 16:43:25 +02:00
index 00000000..93ce3b14
2017-09-22 12:47:50 +02:00
--- /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>
2018-07-18 16:43:25 +02:00
+ <Root level="info">
2017-09-22 12:47:50 +02:00
+ <AppenderRef ref="TerminalConsole" />
+ <AppenderRef ref="File" />
2018-07-18 16:43:25 +02:00
+ </Root>
2017-09-22 12:47:50 +02:00
+ </Loggers>
+</Configuration>
diff --git a/pom.xml b/pom.xml
2021-04-10 23:13:51 +02:00
index 12c285f2..56bb4a7a 100644
2017-09-22 12:47:50 +02:00
--- 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
2021-04-12 08:37:52 +02:00
index 31e172c7..ca1fc13b 100644
2017-09-22 12:47:50 +02:00
--- a/proxy/pom.xml
+++ b/proxy/pom.xml
2021-04-12 08:37:52 +02:00
@@ -71,7 +71,7 @@
2017-09-22 12:47:50 +02:00
</dependency>
<dependency>
<groupId>io.github.waterfallmc</groupId>
- <artifactId>waterfall-log</artifactId>
+ <artifactId>waterfall-log4j</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
2021-04-12 08:37:52 +02:00
@@ -130,6 +130,19 @@
2021-04-10 23:13:51 +02:00
<version>1.7.30</version>
<scope>compile</scope>
2018-07-06 23:00:08 +02:00
</dependency>
+ <!-- Waterfall start - Console improvements -->
+ <dependency>
+ <groupId>net.minecrell</groupId>
+ <artifactId>terminalconsoleappender</artifactId>
2019-07-21 16:25:47 +02:00
+ <version>1.2.0</version>
2018-07-06 23:00:08 +02:00
+ </dependency>
+ <dependency>
2019-07-21 16:25:47 +02:00
+ <groupId>org.jline</groupId>
+ <artifactId>jline-terminal-jansi</artifactId>
+ <version>3.12.1</version>
2018-07-06 23:00:08 +02:00
+ <scope>runtime</scope>
+ </dependency>
+ <!-- Waterfall end -->
</dependencies>
<build>
2017-09-26 18:50:48 +02:00
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
2017-09-22 12:47:50 +02:00
new file mode 100644
2018-07-06 23:00:08 +02:00
index 00000000..765d24bc
2017-09-22 12:47:50 +02:00
--- /dev/null
2017-09-26 18:50:48 +02:00
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
2018-07-06 23:00:08 +02:00
@@ -0,0 +1,39 @@
2017-09-26 18:50:48 +02:00
+package io.github.waterfallmc.waterfall.console;
2017-09-22 12:47:50 +02:00
+
+import net.md_5.bungee.BungeeCord;
+import net.md_5.bungee.api.ChatColor;
+import net.md_5.bungee.api.ProxyServer;
2018-07-06 23:00:08 +02:00
+import net.md_5.bungee.api.chat.ComponentBuilder;
+import net.minecrell.terminalconsole.SimpleTerminalConsole;
2017-09-22 12:47:50 +02:00
+import org.jline.reader.LineReader;
+import org.jline.reader.LineReaderBuilder;
+
2018-07-06 23:00:08 +02:00
+public final class WaterfallConsole extends SimpleTerminalConsole {
2017-09-22 12:47:50 +02:00
+
2018-07-06 23:00:08 +02:00
+ @Override
+ protected LineReader buildReader(LineReaderBuilder builder) {
+ ProxyServer proxy = ProxyServer.getInstance();
+ return super.buildReader(builder
+ .appName(proxy.getName())
+ );
2017-09-22 12:47:50 +02:00
+ }
+
2018-07-06 23:00:08 +02:00
+ @Override
+ protected boolean isRunning() {
+ return BungeeCord.getInstance().isRunning;
2017-09-22 12:47:50 +02:00
+ }
+
2018-07-06 23:00:08 +02:00
+ @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());
2017-09-22 12:47:50 +02:00
+ }
+ }
+
2018-07-06 23:00:08 +02:00
+ @Override
+ protected void shutdown() {
+ ProxyServer.getInstance().stop();
2017-09-22 12:47:50 +02:00
+ }
+
+}
diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
2020-07-01 11:39:43 +02:00
index 8c933123..d71556a7 100644
2017-09-22 12:47:50 +02:00
--- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
+++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java
2020-03-11 16:38:30 +01:00
@@ -46,7 +46,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
2017-09-22 12:47:50 +02:00
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;
2020-03-11 16:38:30 +01:00
@@ -79,14 +78,11 @@ import net.md_5.bungee.command.CommandEnd;
2018-01-07 18:12:05 +01:00
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;
2017-09-22 12:47:50 +02:00
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;
2020-07-01 11:39:43 +02:00
@@ -95,7 +91,6 @@ import net.md_5.bungee.protocol.packet.PluginMessage;
2017-09-22 12:47:50 +02:00
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.
2020-07-01 11:39:43 +02:00
@@ -156,8 +151,12 @@ public class BungeeCord extends ProxyServer
2017-09-22 12:47:50 +02:00
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()
2020-07-01 11:39:43 +02:00
@@ -208,6 +207,8 @@ public class BungeeCord extends ProxyServer
2019-10-27 18:22:28 +01:00
// BungeeCord. This version is only used when extracting the libraries to their temp folder.
System.setProperty( "library.jansi.version", "BungeeCord" );
2017-09-22 12:47:50 +02:00
+ // Waterfall start - Use TerminalConsoleAppender and Log4J
+ /*
2019-10-27 18:22:28 +01:00
AnsiConsole.systemInstall();
consoleReader = new ConsoleReader();
consoleReader.setExpandEvents( false );
2020-07-01 11:39:43 +02:00
@@ -216,6 +217,9 @@ public class BungeeCord extends ProxyServer
2017-10-08 20:57:57 +02:00
logger = new BungeeLogger( "BungeeCord", "proxy.log", consoleReader );
2017-09-22 12:47:50 +02:00
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
2018-07-09 11:12:55 +02:00
pluginManager = new PluginManager( this );
getPluginManager().registerCommand( null, new CommandReload() );
2018-12-21 16:24:26 +01:00
diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java b/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
2021-01-29 11:35:23 +01:00
index a4fe8aad..e830198d 100644
2018-12-21 16:24:26 +01:00
--- a/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
+++ b/proxy/src/main/java/net/md_5/bungee/BungeeCordLauncher.java
2019-03-20 04:55:00 +01:00
@@ -63,6 +63,9 @@ public class BungeeCordLauncher
2018-12-21 16:24:26 +01:00
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 )
{
2019-03-20 04:55:00 +01:00
@@ -71,6 +74,8 @@ public class BungeeCordLauncher
2018-12-21 16:24:26 +01:00
bungee.getConsole().sendMessage( new ComponentBuilder( "Command not found" ).color( ChatColor.RED ).create() );
}
}
+ */
+ // Waterfall end
}
}
}
2018-01-07 18:12:05 +01:00
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 );
- }
-}
2017-09-22 12:47:50 +02:00
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
2017-10-28 21:51:33 +02:00
index 10366c88..8c5cc949 100644
2017-09-22 12:47:50 +02:00
--- 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;
}
--
2021-04-10 23:13:51 +02:00
2.31.0
2017-09-22 12:47:50 +02:00