From cafe3667542bafd7d9ea0b71c139b2507e4c61ff Mon Sep 17 00:00:00 2001 From: Sotr Date: Sun, 26 Jan 2020 00:54:26 +0700 Subject: [PATCH] Log4j and nojline support Former-commit-id: ccbaaf50b605a3596e065e739bc59c1d76ae6adf --- pom.xml | 13 +- .../Util/Logger/QuickShopLogger.java | 285 ++++++++++++------ src/main/resources/config.yml | 3 + 3 files changed, 204 insertions(+), 97 deletions(-) diff --git a/pom.xml b/pom.xml index c3b42685c..4df5deda9 100644 --- a/pom.xml +++ b/pom.xml @@ -104,8 +104,7 @@ org.apache.maven.plugins maven-dependency-plugin - 3.1.1 - + @@ -121,6 +120,10 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + central-repo + https://repo1.maven.org/maven2/ + reserve-repo https://dl.bintray.com/theneweconomy/java/ @@ -357,6 +360,12 @@ 1.3 compile + + jline + jline + 2.14.6 + provided + org.slf4j slf4j-api diff --git a/src/main/java/org/maxgamer/quickshop/Util/Logger/QuickShopLogger.java b/src/main/java/org/maxgamer/quickshop/Util/Logger/QuickShopLogger.java index e05bb138b..ae264f184 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Logger/QuickShopLogger.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Logger/QuickShopLogger.java @@ -1,20 +1,17 @@ /* - * This file is a part of project QuickShop, the name is QuickShopLogger.java - * Copyright (C) Ghost_chu - * Copyright (C) Bukkit Commons Studio and contributors + * This file is a part of project QuickShop, the name is QuickShopLogger.java Copyright (C) + * Ghost_chu Copyright (C) Bukkit Commons Studio and contributors * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU Lesser General Public License as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU Lesser General Public License along with this program. + * If not, see . */ package org.maxgamer.quickshop.Util.Logger; @@ -57,60 +54,102 @@ import org.apache.logging.log4j.LogManager; import org.bukkit.ChatColor; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginLogger; -import org.fusesource.jansi.Ansi; +import org.jetbrains.annotations.Nullable; +import org.maxgamer.quickshop.NonQuickShopStuffs.de.Keyle.MyPet.api.util.ReflectionUtil; +import org.maxgamer.quickshop.Util.Util; +import com.google.common.collect.Maps; /* - * Originally take from Mypet which is a awesome project, - * extends PluginLogger in order to replace the default logger. + * Originally take from Mypet which is a awesome project, extends PluginLogger in order to replace + * the default logger. * - * This is generally a new built logger system that - * rely on the Java logger to provide better customization. + * This is generally a new built logger system that rely on the Java logger to provide better + * customization. * * It is possible to switch to Log4j for better performance in the future. */ public class QuickShopLogger extends PluginLogger { protected boolean debugSetup = false; + @Nullable + private org.apache.logging.log4j.Logger log4jLogger; /** Mapping from the text pattern of Bukkit color to the corresponding text format of Ansi */ private Map bukkitToAnsi; + private Map levelMapping; + + private void registerLevels() { + levelMapping = Maps.newHashMap(); + + levelMapping.put(Level.ALL, org.apache.logging.log4j.Level.ALL); + levelMapping.put(Level.CONFIG, org.apache.logging.log4j.Level.DEBUG); + levelMapping.put(Level.FINE, org.apache.logging.log4j.Level.DEBUG); + levelMapping.put(Level.FINER, org.apache.logging.log4j.Level.TRACE); + levelMapping.put(Level.FINEST, org.apache.logging.log4j.Level.TRACE); + levelMapping.put(Level.INFO, org.apache.logging.log4j.Level.INFO); + levelMapping.put(Level.WARNING, org.apache.logging.log4j.Level.WARN); + levelMapping.put(Level.SEVERE, org.apache.logging.log4j.Level.ERROR); + levelMapping.put(Level.OFF, org.apache.logging.log4j.Level.OFF); + } + // below are non-static for secret optimization /** Regex that indicates the case insensitive */ private String IGNORE_CASE; // private FileHandler debugLogFileHandler = null; - private boolean AnsiSupported = true; + private boolean hasAnsi = true; + private boolean hasJline = true; + private boolean useLog4j; @SneakyThrows public QuickShopLogger(Plugin plugin) { super(plugin); - - // Ansi setup - try { - if (Ansi.isEnabled()) registerStyles(); - } catch (NoClassDefFoundError e) { - AnsiSupported = false; - info("Your server doesn't support ANSI color, disabled color formatter."); - } - + registerStyles(); + // Logger re-naming String prefix = plugin.getDescription().getPrefix(); - String pluginName = - prefix != null - ? "[" + ChatColor.YELLOW + prefix + ChatColor.RESET + "] " - : "[" + ChatColor.YELLOW + plugin.getDescription().getName() + ChatColor.RESET + "] "; - pluginName = AnsiSupported ? applyStyles(pluginName) : pluginName; + String pluginName = (useLog4j = plugin.getConfig().getBoolean("enable-log4j")) ? + (ChatColor.YELLOW + (prefix == null ? plugin.getDescription().getName() : prefix) + + ChatColor.RESET) + : + + (prefix != null ? "[" + ChatColor.YELLOW + prefix + ChatColor.RESET + "] " + : "[" + ChatColor.YELLOW + plugin.getDescription().getName() + ChatColor.RESET + "] "); + pluginName = applyStyles(pluginName); + + // Log4j setup + if (useLog4j) { + registerLevels(); + log4jLogger = LogManager.getLogger(pluginName); + info("Log4J has been enabled as logging system."); + } else { + // Remove logger name from package name + Field nameField = Logger.class.getDeclaredField("name"); + nameField.setAccessible(true); // private + nameField.set(this, ""); + } + // Apply plugin name for BukkitLogger Field pluginNameField = PluginLogger.class.getDeclaredField("pluginName"); pluginNameField.setAccessible(true); // private pluginNameField.set(this, pluginName); - // Remove logger name from package name - Field nameField = Logger.class.getDeclaredField("name"); - nameField.setAccessible(true); // private - nameField.set(this, ""); + // Ansi setup + try { + hasAnsi = org.fusesource.jansi.Ansi.isEnabled(); + } catch (NoClassDefFoundError e) { + hasAnsi = false; + info("Your server do not support Ansi, colour formatter will not be applied."); + } + + Class main = Class.forName("org.bukkit.craftbukkit.Main"); // Not in subversion + Field useJline = main.getField("useJline"); + hasJline = useJline.getBoolean(null); + if (!hasJline) { + info("As you have turned Jline off, colour formatter will not be applied."); + } this.config(); // super.setUseParentHandlers(false); @@ -121,17 +160,27 @@ public class QuickShopLogger extends PluginLogger { public void log(LogRecord logRecord) { String message = logRecord.getMessage(); - if (message != null && AnsiSupported) { - if (logRecord.getLevel() == Level.WARNING) { - message = ChatColor.YELLOW + message; + if (message == null) { + return; + } else { + + if (useLog4j) { + log4jLogger.log( + levelMapping.getOrDefault(logRecord.getLevel(), org.apache.logging.log4j.Level.INFO), + applyStyles(message)); + + } else { + if (logRecord.getLevel() == Level.WARNING) { + message = ChatColor.YELLOW + message; + } else if (logRecord.getLevel() == Level.SEVERE) { + message = ChatColor.RED + message; + } + + logRecord.setMessage(applyStyles(message)); + super.log(logRecord); } - if (logRecord.getLevel() == Level.SEVERE) { - message = ChatColor.RED + message; - } - message = applyStyles(message); - logRecord.setMessage(message); + } - super.log(logRecord); } /** @@ -145,52 +194,82 @@ public class QuickShopLogger extends PluginLogger { } public void info(Object... params) { - this.info(collectParams(params)); + if (useLog4j) { + log4jLogger.info(collectParams(params)); + } else { + super.info(collectParams(params)); + } } public void warning(Object... params) { - this.warning(collectParams(params)); + if (useLog4j) { + log4jLogger.warn(collectParams(params)); + } else { + super.warning(collectParams(params)); + } } public void severe(Object... params) { - this.severe(collectParams(params)); + if (useLog4j) { + log4jLogger.error(collectParams(params)); + } else { + super.severe(collectParams(params)); + } } public void config(Object... params) { - this.config(collectParams(params)); + if (useLog4j) { + log4jLogger.debug(collectParams(params)); + } else { + super.config(collectParams(params)); + } } public void fine(Object... params) { - this.fine(collectParams(params)); + if (useLog4j) { + log4jLogger.debug(collectParams(params)); + } else { + super.fine(collectParams(params)); + } } public void finer(Object... params) { - this.finer(collectParams(params)); + if (useLog4j) { + log4jLogger.trace(collectParams(params)); + } else { + super.finer(collectParams(params)); + } } public void finest(Object... params) { - this.finest(collectParams(params)); + if (useLog4j) { + log4jLogger.trace(collectParams(params)); + } else { + super.finest(collectParams(params)); + } } // Style stuffs - /** * Apply Ansi styples to the specific message if it contains, internally converting Bukkit style - * color text to Ansi code. + * color text to Ansi code or empty string if Ansi is not available. * * @param message to apply styles * @return text maybe applied styles */ public String applyStyles(String message) { - for (Entry entry : bukkitToAnsi.entrySet()) - message = entry.getKey().matcher(message).replaceAll(entry.getValue()); + for (Entry entry : bukkitToAnsi.entrySet()) { + message = + entry.getKey().matcher(message).replaceAll(hasAnsi && hasJline ? entry.getValue() : ""); + } - return message.concat(Ansi.ansi().reset().toString()); + return hasAnsi && hasJline ? message.concat(org.fusesource.jansi.Ansi.ansi().reset().toString()) + : message; } /* - * This will compile patterns for Bukkit colors as the key, - * and stringify Ansi as the value, then register in to the system. + * This will compile patterns for Bukkit colors as the key, and stringify Ansi as the value, then + * register in to the system. */ private void registerStyles() { // Initial here for secert optimization @@ -198,56 +277,67 @@ public class QuickShopLogger extends PluginLogger { IGNORE_CASE = "(?i)"; // Colors - regAnsiMapping(BLACK, Ansi.Color.BLACK); - regAnsiMapping(DARK_BLUE, Ansi.Color.BLUE); - regAnsiMapping(DARK_GREEN, Ansi.Color.GREEN); - regAnsiMapping(DARK_AQUA, Ansi.Color.CYAN); - regAnsiMapping(DARK_RED, Ansi.Color.RED); - regAnsiMapping(DARK_PURPLE, Ansi.Color.MAGENTA); - regAnsiMapping(GOLD, Ansi.Color.YELLOW); - regAnsiMapping(GRAY, Ansi.Color.WHITE); - regAnsiMapping(DARK_GRAY, Ansi.Color.BLACK, true); - regAnsiMapping(BLUE, Ansi.Color.BLUE, true); - regAnsiMapping(GREEN, Ansi.Color.GREEN, true); - regAnsiMapping(AQUA, Ansi.Color.CYAN, true); - regAnsiMapping(RED, Ansi.Color.RED, true); - regAnsiMapping(LIGHT_PURPLE, Ansi.Color.MAGENTA, true); - regAnsiMapping(YELLOW, Ansi.Color.YELLOW, true); - regAnsiMapping(WHITE, Ansi.Color.WHITE, true); + regAnsiMapping(BLACK, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.BLACK); + regAnsiMapping(DARK_BLUE, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.BLUE); + regAnsiMapping(DARK_GREEN, + !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.GREEN); + regAnsiMapping(DARK_AQUA, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.CYAN); + regAnsiMapping(DARK_RED, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.RED); + regAnsiMapping(DARK_PURPLE, + !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.MAGENTA); + regAnsiMapping(GOLD, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.YELLOW); + regAnsiMapping(GRAY, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.WHITE); + regAnsiMapping(DARK_GRAY, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.BLACK, + true); + regAnsiMapping(BLUE, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.BLUE, + true); + regAnsiMapping(GREEN, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.GREEN, + true); + regAnsiMapping(AQUA, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.CYAN, + true); + regAnsiMapping(RED, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.RED, true); + regAnsiMapping(LIGHT_PURPLE, + !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.MAGENTA, true); + regAnsiMapping(YELLOW, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.YELLOW, + true); + regAnsiMapping(WHITE, !(hasAnsi && hasJline) ? null : org.fusesource.jansi.Ansi.Color.WHITE, + true); // Effects - regAnsiMapping(MAGIC, Ansi.Attribute.BLINK_SLOW); - regAnsiMapping(BOLD, Ansi.Attribute.UNDERLINE_DOUBLE); - regAnsiMapping(STRIKETHROUGH, Ansi.Attribute.STRIKETHROUGH_ON); - regAnsiMapping(UNDERLINE, Ansi.Attribute.UNDERLINE); - regAnsiMapping(ITALIC, Ansi.Attribute.ITALIC); - regAnsiMapping(RESET, Ansi.Attribute.RESET); + regAnsiMapping(MAGIC, org.fusesource.jansi.Ansi.Attribute.BLINK_SLOW); + regAnsiMapping(BOLD, org.fusesource.jansi.Ansi.Attribute.UNDERLINE_DOUBLE); + regAnsiMapping(STRIKETHROUGH, org.fusesource.jansi.Ansi.Attribute.STRIKETHROUGH_ON); + regAnsiMapping(UNDERLINE, org.fusesource.jansi.Ansi.Attribute.UNDERLINE); + regAnsiMapping(ITALIC, org.fusesource.jansi.Ansi.Attribute.ITALIC); + regAnsiMapping(RESET, org.fusesource.jansi.Ansi.Attribute.RESET); } /* * Register a mapping from Bukkit color to Ansi */ - private void regAnsiMapping(ChatColor bukkColor, Ansi.Color ansiColor) { + private void regAnsiMapping(ChatColor bukkColor, org.fusesource.jansi.Ansi.Color ansiColor) { regAnsiMapping0(toPattern(bukkColor), toDesc(ansiColor)); } /* * Register a mapping from Bukkit color to Ansi */ - private void regAnsiMapping(ChatColor bukkColor, Ansi.Attribute ansiAttribute) { + private void regAnsiMapping(ChatColor bukkColor, + org.fusesource.jansi.Ansi.Attribute ansiAttribute) { regAnsiMapping0(toPattern(bukkColor), toDesc(ansiAttribute)); } /* * Register a mapping from Bukkit color to Ansi, with color option */ - private void regAnsiMapping(ChatColor bukkColor, Ansi.Color ansiColor, boolean intensity) { + private void regAnsiMapping(ChatColor bukkColor, org.fusesource.jansi.Ansi.Color ansiColor, + boolean intensity) { regAnsiMapping0(toPattern(bukkColor), toDesc(ansiColor, intensity)); } /* - * Register a mapping from the pattern of Bukkit color to the description of Ansi, - * and this is the genuine type for them to be registered. + * Register a mapping from the pattern of Bukkit color to the description of Ansi, and this is the + * genuine type for them to be registered. */ private void regAnsiMapping0(Pattern bukkitPattern, String ansiDesc) { this.bukkitToAnsi.put(bukkitPattern, ansiDesc); @@ -269,8 +359,9 @@ public class QuickShopLogger extends PluginLogger { * @param ansiColor * @return Ansi with reset ahead */ - private Ansi resetWith(Ansi.Color ansiColor) { - return Ansi.ansi().a(Ansi.Attribute.RESET).fg(ansiColor); + private org.fusesource.jansi.Ansi resetWith(org.fusesource.jansi.Ansi.Color ansiColor) { + return org.fusesource.jansi.Ansi.ansi().a(org.fusesource.jansi.Ansi.Attribute.RESET) + .fg(ansiColor); } /** @@ -280,8 +371,10 @@ public class QuickShopLogger extends PluginLogger { * @param intensity Ansi color option * @return stringified Ansi */ - private String toDesc(Ansi.Color ansiColor, boolean intensity) { - return intensity ? resetWith(ansiColor).bold().toString() : toDesc(ansiColor); + private String toDesc(org.fusesource.jansi.Ansi.Color ansiColor, boolean intensity) { + return hasAnsi && hasJline + ? (intensity ? resetWith(ansiColor).bold().toString() : toDesc(ansiColor)) + : ""; } /** @@ -290,8 +383,8 @@ public class QuickShopLogger extends PluginLogger { * @param ansiColor Ansi * @return stringified Ansi */ - private String toDesc(Ansi.Color ansiColor) { - return resetWith(ansiColor).boldOff().toString(); + private String toDesc(org.fusesource.jansi.Ansi.Color ansiColor) { + return hasAnsi && hasJline ? resetWith(ansiColor).boldOff().toString() : ""; } /** @@ -300,7 +393,9 @@ public class QuickShopLogger extends PluginLogger { * @param ansiAttribute Ansi * @return stringified Ansi */ - private String toDesc(Ansi.Attribute ansiAttribute) { - return Ansi.ansi().a(Ansi.Attribute.RESET).toString(); + private String toDesc(org.fusesource.jansi.Ansi.Attribute ansiAttribute) { + return hasAnsi && hasJline + ? org.fusesource.jansi.Ansi.ansi().a(org.fusesource.jansi.Ansi.Attribute.RESET).toString() + : ""; } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c17c01c72..8de72e3b7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -89,6 +89,9 @@ send-shop-protection-alert: true #Disable the debug silent logger. This should can fix heavy lagging on some overloaded server, but AFTER ENABLE IT, YOU WILL LOSE OUR SUPPORT UNTIL YOU RE-ENABLE IT. disable-debuglogger: false +# Log4j provides better logging performance, turn this off when you are suffering logging issues. +enable-log4j: true + #Force plugin running under a platform #0=AUTO #1=SPIGOT