From e52a461a73adf56bb16393454cff102790903153 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sun, 1 Mar 2020 10:48:49 -0600 Subject: [PATCH 1/2] Attempt to get log4j2 working --- bootstrap/standalone/pom.xml | 58 ++++++++--- .../platform/standalone/GeyserBootstrap.java | 13 +-- .../console/ConsoleCommandReader.java | 96 ------------------- .../standalone/console/GeyserLogger.java | 39 ++++++-- .../resources/log4j2.component.properties | 1 + .../standalone/src/main/resources/log4j2.yml | 20 ++++ connector/pom.xml | 2 +- 7 files changed, 98 insertions(+), 131 deletions(-) delete mode 100644 bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/ConsoleCommandReader.java create mode 100644 bootstrap/standalone/src/main/resources/log4j2.component.properties create mode 100644 bootstrap/standalone/src/main/resources/log4j2.yml diff --git a/bootstrap/standalone/pom.xml b/bootstrap/standalone/pom.xml index 2e767de5e..800a79c3e 100644 --- a/bootstrap/standalone/pom.xml +++ b/bootstrap/standalone/pom.xml @@ -20,27 +20,55 @@ net.minecrell terminalconsoleappender - 1.0.0 - jar - compile + 1.1.1 + + + org.apache.logging.log4j + log4j-core + + + org.jline + jline-reader + + + org.jline + jline-terminal-jna + + + org.jline + jline-terminal + + - org.slf4j - slf4j-api - 1.7.5 - compile + org.jline + jline-terminal + 3.9.0 - org.slf4j - slf4j-simple - 1.6.4 - compile + org.jline + jline-terminal-jna + 3.9.0 - org.fusesource.jansi - jansi - 1.18 - compile + org.jline + jline-reader + 3.9.0 + + + org.apache.logging.log4j + log4j-api + 2.13.1 + + + org.apache.logging.log4j + log4j-core + 2.13.1 + + + org.apache.logging.log4j + log4j-slf4j18-impl + 2.13.1 diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java index a99b2434e..199745388 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java @@ -25,12 +25,10 @@ package org.geysermc.platform.standalone; -import org.fusesource.jansi.AnsiConsole; import org.geysermc.common.PlatformType; import org.geysermc.common.bootstrap.IGeyserBootstrap; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.utils.FileUtils; -import org.geysermc.platform.standalone.console.ConsoleCommandReader; import org.geysermc.platform.standalone.console.GeyserLogger; import java.io.File; @@ -48,11 +46,6 @@ public class GeyserBootstrap implements IGeyserBootstrap { @Override public void onEnable() { - // Metric - if (!(System.console() == null) && System.getProperty("os.name", "Windows 10").toLowerCase().contains("windows")) { - AnsiConsole.systemInstall(); - } - geyserLogger = new GeyserLogger(); try { @@ -63,10 +56,8 @@ public class GeyserBootstrap implements IGeyserBootstrap { System.exit(0); } - GeyserConnector connector = GeyserConnector.start(PlatformType.STANDALONE, this); - - ConsoleCommandReader consoleReader = new ConsoleCommandReader(connector); - consoleReader.startConsole(); + GeyserConnector.start(PlatformType.STANDALONE, this); + geyserLogger.start(); } @Override diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/ConsoleCommandReader.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/ConsoleCommandReader.java deleted file mode 100644 index ff6cff798..000000000 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/ConsoleCommandReader.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * @author GeyserMC - * @link https://github.com/GeyserMC/Geyser - */ - -package org.geysermc.platform.standalone.console; - -import net.minecrell.terminalconsole.TerminalConsoleAppender; - -import org.geysermc.connector.GeyserConnector; -import org.geysermc.connector.command.GeyserConsoleCommandSender; -import org.jline.reader.EndOfFileException; -import org.jline.reader.LineReader; -import org.jline.reader.LineReaderBuilder; -import org.jline.reader.UserInterruptException; -import org.jline.terminal.Terminal; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class ConsoleCommandReader { - - private GeyserConnector connector; - private Terminal terminal; - - public ConsoleCommandReader(GeyserConnector connector) { - this.connector = connector; - this.terminal = TerminalConsoleAppender.getTerminal(); - } - - public void startConsole() { - Thread thread = new Thread(() -> { - if (terminal != null) { - LineReader lineReader = LineReaderBuilder.builder() - .appName("Geyser") - .terminal(terminal) - .build(); - TerminalConsoleAppender.setReader(lineReader); - - try { - String line; - - while (true) { - try { - line = lineReader.readLine("> "); - } catch (EndOfFileException ignored) { - continue; - } - - if (line == null) - break; - } - } catch (UserInterruptException ignore) { - /* do nothing */ - } finally { - TerminalConsoleAppender.setReader(null); - } - } else { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { - String line; - while ((line = reader.readLine()) != null) { - GeyserConsoleCommandSender sender = new GeyserConsoleCommandSender(); - connector.getCommandMap().runCommand(sender, line); - } - } catch (IOException ex) { - Logger.getLogger("Geyser").log(Level.SEVERE, null, ex); - } - } - }); - - thread.setName("ConsoleCommandThread"); - } -} diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java index 35347e7ae..3ec11cc07 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java @@ -27,15 +27,22 @@ package org.geysermc.platform.standalone.console; import io.sentry.Sentry; +import lombok.extern.log4j.Log4j2; + +import net.minecrell.terminalconsole.SimpleTerminalConsole; + import org.geysermc.common.ChatColor; import org.geysermc.common.logger.IGeyserLogger; +import org.geysermc.connector.GeyserConnector; +import org.geysermc.connector.command.GeyserConsoleCommandSender; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.logging.*; -public class GeyserLogger implements IGeyserLogger { +@Log4j2 +public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger { private boolean colored = true; private boolean debug = false; @@ -88,40 +95,56 @@ public class GeyserLogger implements IGeyserLogger { } } + @Override + protected boolean isRunning() { + return !GeyserConnector.getInstance().isShuttingDown(); + } + + @Override + protected void runCommand(String line) { + GeyserConsoleCommandSender sender = new GeyserConsoleCommandSender(); + GeyserConnector.getInstance().getCommandMap().runCommand(sender, line); + } + + @Override + protected void shutdown() { + GeyserConnector.getInstance().shutdown(); + } + @Override public void severe(String message) { - System.out.println(printConsole(ChatColor.DARK_RED + message, colored)); + log.fatal(printConsole(ChatColor.DARK_RED + message, colored)); } @Override public void severe(String message, Throwable error) { - System.out.println(printConsole(ChatColor.DARK_RED + message + "\n" + error.getMessage(), colored)); + log.fatal(printConsole(ChatColor.DARK_RED + message, colored), error); } @Override public void error(String message) { - System.out.println(printConsole(ChatColor.RED + message, colored)); + log.error(printConsole(ChatColor.RED + message, colored)); } @Override public void error(String message, Throwable error) { - System.out.println(printConsole(ChatColor.RED + message + "\n" + error, colored)); + log.error(printConsole(ChatColor.RED + message, colored), error); } @Override public void warning(String message) { - System.out.println(printConsole(ChatColor.YELLOW + message, colored)); + log.warn(printConsole(ChatColor.YELLOW + message, colored)); } @Override public void info(String message) { - System.out.println(printConsole(ChatColor.WHITE + message, colored)); + log.info(printConsole(ChatColor.WHITE + message, colored)); } @Override public void debug(String message) { if (debug) - System.out.println(printConsole(ChatColor.GRAY + message, colored)); + log.info(printConsole(ChatColor.GRAY + message, colored)); } public static String printConsole(String message, boolean colors) { diff --git a/bootstrap/standalone/src/main/resources/log4j2.component.properties b/bootstrap/standalone/src/main/resources/log4j2.component.properties new file mode 100644 index 000000000..ed03a2b82 --- /dev/null +++ b/bootstrap/standalone/src/main/resources/log4j2.component.properties @@ -0,0 +1 @@ +log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector \ No newline at end of file diff --git a/bootstrap/standalone/src/main/resources/log4j2.yml b/bootstrap/standalone/src/main/resources/log4j2.yml new file mode 100644 index 000000000..c839ef8dc --- /dev/null +++ b/bootstrap/standalone/src/main/resources/log4j2.yml @@ -0,0 +1,20 @@ +Configuration: + Appenders: + TerminalConsole: + PatternLayout: + pattern: "%cyan{%d{HH:mm:ss}} [%style{%highlight{%-5level}{STYLE=Logback}}] %minecraftFormatting{%msg}%n" + name: Console + RollingRandomAccessFile: + PatternLayout: + pattern: "%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level - %msg%n" + Policies: + TimeBasedTriggeringPolicy: "" + OnStartupTriggeringPolicy: "" + name: File + fileName: "logs/server.log" + filePattern: "logs/%d{yyyy-MM-dd}-%i.log.gz" + Loggers: + Root: + level: error + AppenderRef: + ref: Console \ No newline at end of file diff --git a/connector/pom.xml b/connector/pom.xml index 31bbcc4b6..e9de81e64 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -57,7 +57,7 @@ com.github.steveice10 packetlib - 1.4-SNAPSHOT + 1.5-SNAPSHOT compile From 936cbad67d918d27b17643fbf4c85a3e6f0d547d Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Wed, 4 Mar 2020 20:35:23 -0600 Subject: [PATCH 2/2] Get log4j2 working and fix commands --- bootstrap/standalone/pom.xml | 28 ++++++- .../standalone/console/GeyserLogger.java | 79 ++++--------------- .../resources/log4j2.component.properties | 1 - .../standalone/src/main/resources/log4j2.xml | 21 +++++ .../standalone/src/main/resources/log4j2.yml | 20 ----- .../connector/command/GeyserCommandMap.java | 4 +- .../command/GeyserConsoleCommandSender.java | 41 ---------- .../command/defaults/HelpCommand.java | 2 +- 8 files changed, 67 insertions(+), 129 deletions(-) delete mode 100644 bootstrap/standalone/src/main/resources/log4j2.component.properties create mode 100644 bootstrap/standalone/src/main/resources/log4j2.xml delete mode 100644 bootstrap/standalone/src/main/resources/log4j2.yml delete mode 100644 connector/src/main/java/org/geysermc/connector/command/GeyserConsoleCommandSender.java diff --git a/bootstrap/standalone/pom.xml b/bootstrap/standalone/pom.xml index 800a79c3e..7c271339a 100644 --- a/bootstrap/standalone/pom.xml +++ b/bootstrap/standalone/pom.xml @@ -90,6 +90,13 @@ org.apache.maven.plugins maven-shade-plugin 3.2.1 + + + com.github.edwgiz + maven-shade-plugin.log4j2-cachefile-transformer + 2.8.1 + + package @@ -97,10 +104,29 @@ shade - true + false + + + + *:* + + META-INF/versions/9/module-info.class + + + + + + org.geysermc.platform.standalone.GeyserBootstrap + + + + + ${project.build.directory}/dependency-reduced-pom.xml + diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java index 3ec11cc07..cac40b563 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/console/GeyserLogger.java @@ -25,75 +25,20 @@ package org.geysermc.platform.standalone.console; -import io.sentry.Sentry; - import lombok.extern.log4j.Log4j2; import net.minecrell.terminalconsole.SimpleTerminalConsole; +import org.apache.logging.log4j.core.config.Configurator; import org.geysermc.common.ChatColor; import org.geysermc.common.logger.IGeyserLogger; import org.geysermc.connector.GeyserConnector; -import org.geysermc.connector.command.GeyserConsoleCommandSender; - -import java.io.File; -import java.io.IOException; -import java.util.Date; -import java.util.logging.*; +import org.geysermc.connector.command.CommandSender; @Log4j2 -public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger { +public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger, CommandSender { private boolean colored = true; - private boolean debug = false; - - public GeyserLogger() { - ConsoleHandler consoleHandler = new ConsoleHandler(); - consoleHandler.setLevel(Level.INFO); - consoleHandler.setFormatter(new SimpleFormatter() { - private static final String format = "[%1$tT][%2$-5s] %3$s %n"; - - @Override - public synchronized String format(LogRecord lr) { - return String.format(format, - new Date(lr.getMillis()), - lr.getLevel().getLocalizedName(), - lr.getMessage() - ); - } - }); - - try { - File logDir = new File("logs"); - logDir.mkdir(); - File logFile = new File(logDir, "latest.log"); - int maxLogFileSize = 20;//Mo - if (logFile.exists() && (logFile.length()) > maxLogFileSize * 1024L * 1024L) - this.warning("Your log file is larger than " + maxLogFileSize + "Mo, you should backup and clean it !"); - FileHandler fileHandler = new FileHandler(logFile.getCanonicalPath(), true); - fileHandler.setLevel(Level.INFO); - fileHandler.setFormatter(new SimpleFormatter() { - private static final String format = "[%1$tF %1$tT][%2$-5s] %3$s %n"; - - @Override - public synchronized String format(LogRecord lr) { - return String.format(format, - new Date(lr.getMillis()), - lr.getLevel().getLocalizedName(), - lr.getMessage() - ); - } - }); - } catch (IOException | SecurityException ex) { - Logger.getLogger(GeyserLogger.class.getName()).log(Level.SEVERE, null, ex); - } - - if (System.getenv().containsKey("DP_SENTRY_CLIENT_KEY")) { - Handler sentryHandler = new io.sentry.jul.SentryHandler(); - sentryHandler.setLevel(Level.SEVERE); - Sentry.init(System.getenv().get("DP_SENTRY_CLIENT_KEY")); - } - } @Override protected boolean isRunning() { @@ -102,8 +47,7 @@ public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger @Override protected void runCommand(String line) { - GeyserConsoleCommandSender sender = new GeyserConsoleCommandSender(); - GeyserConnector.getInstance().getCommandMap().runCommand(sender, line); + GeyserConnector.getInstance().getCommandMap().runCommand(this, line); } @Override @@ -143,8 +87,7 @@ public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger @Override public void debug(String message) { - if (debug) - log.info(printConsole(ChatColor.GRAY + message, colored)); + log.debug(printConsole(ChatColor.GRAY + message, colored)); } public static String printConsole(String message, boolean colors) { @@ -153,6 +96,16 @@ public class GeyserLogger extends SimpleTerminalConsole implements IGeyserLogger @Override public void setDebug(boolean debug) { - this.debug = debug; + Configurator.setLevel(log.getName(), debug ? org.apache.logging.log4j.Level.DEBUG : log.getLevel()); + } + + @Override + public String getName() { + return "CONSOLE"; + } + + @Override + public void sendMessage(String message) { + info(message); } } diff --git a/bootstrap/standalone/src/main/resources/log4j2.component.properties b/bootstrap/standalone/src/main/resources/log4j2.component.properties deleted file mode 100644 index ed03a2b82..000000000 --- a/bootstrap/standalone/src/main/resources/log4j2.component.properties +++ /dev/null @@ -1 +0,0 @@ -log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector \ No newline at end of file diff --git a/bootstrap/standalone/src/main/resources/log4j2.xml b/bootstrap/standalone/src/main/resources/log4j2.xml new file mode 100644 index 000000000..957415405 --- /dev/null +++ b/bootstrap/standalone/src/main/resources/log4j2.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bootstrap/standalone/src/main/resources/log4j2.yml b/bootstrap/standalone/src/main/resources/log4j2.yml deleted file mode 100644 index c839ef8dc..000000000 --- a/bootstrap/standalone/src/main/resources/log4j2.yml +++ /dev/null @@ -1,20 +0,0 @@ -Configuration: - Appenders: - TerminalConsole: - PatternLayout: - pattern: "%cyan{%d{HH:mm:ss}} [%style{%highlight{%-5level}{STYLE=Logback}}] %minecraftFormatting{%msg}%n" - name: Console - RollingRandomAccessFile: - PatternLayout: - pattern: "%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level - %msg%n" - Policies: - TimeBasedTriggeringPolicy: "" - OnStartupTriggeringPolicy: "" - name: File - fileName: "logs/server.log" - filePattern: "logs/%d{yyyy-MM-dd}-%i.log.gz" - Loggers: - Root: - level: error - AppenderRef: - ref: Console \ No newline at end of file diff --git a/connector/src/main/java/org/geysermc/connector/command/GeyserCommandMap.java b/connector/src/main/java/org/geysermc/connector/command/GeyserCommandMap.java index 6a8e16808..7172622c3 100644 --- a/connector/src/main/java/org/geysermc/connector/command/GeyserCommandMap.java +++ b/connector/src/main/java/org/geysermc/connector/command/GeyserCommandMap.java @@ -61,7 +61,7 @@ public class GeyserCommandMap { } public void runCommand(CommandSender sender, String command) { - if (!command.startsWith("/geyser ")) + if (!command.startsWith("geyser ")) return; command = command.trim(); @@ -80,7 +80,7 @@ public class GeyserCommandMap { GeyserCommand cmd = commandMap.get(label); if (cmd == null) { - connector.getLogger().warning("Invalid Command! Try /help for a list of commands."); + connector.getLogger().error("Invalid Command! Try /geyser help for a list of commands."); return; } diff --git a/connector/src/main/java/org/geysermc/connector/command/GeyserConsoleCommandSender.java b/connector/src/main/java/org/geysermc/connector/command/GeyserConsoleCommandSender.java deleted file mode 100644 index 20b2f9739..000000000 --- a/connector/src/main/java/org/geysermc/connector/command/GeyserConsoleCommandSender.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * @author GeyserMC - * @link https://github.com/GeyserMC/Geyser - */ - -package org.geysermc.connector.command; - -import org.geysermc.common.ChatColor; - -public class GeyserConsoleCommandSender implements CommandSender { - - @Override - public String getName() { - return "CONSOLE"; - } - - @Override - public void sendMessage(String message) { - System.out.println(ChatColor.toANSI(message + ChatColor.RESET)); - } -} diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/HelpCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/HelpCommand.java index 6964b9a33..4fa0068ae 100644 --- a/connector/src/main/java/org/geysermc/connector/command/defaults/HelpCommand.java +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/HelpCommand.java @@ -52,7 +52,7 @@ public class HelpCommand extends GeyserCommand { Collections.sort(commands); for (String cmd : commands) { - sender.sendMessage(ChatColor.YELLOW + "/" + cmd + ChatColor.WHITE + ": " + cmds.get(cmd).getDescription()); + sender.sendMessage(ChatColor.YELLOW + "/geyser " + cmd + ChatColor.WHITE + ": " + cmds.get(cmd).getDescription()); } } }