From 30fead1dfc41c615d41be4d0b88c3257dbe5689c Mon Sep 17 00:00:00 2001 From: games647 Date: Sun, 21 May 2017 13:07:40 +0200 Subject: [PATCH] Refactor formatter to have less duplicate code + Add random plugin color --- pom.xml | 2 +- .../bukkit/ColorConsoleBukkit.java | 17 +- .../bukkit/ColorPluginAppender.java | 157 ++---------------- .../bungee/ColorConsoleBungee.java | 19 ++- .../bungee/ColorLogFormatter.java | 144 ++++------------ .../colorconsole/common/ColorAppender.java | 55 ++++++ .../colorconsole/common/CommonFormatter.java | 132 +++++++++++++++ .../sponge/ColorConsoleConfig.java | 2 +- .../sponge/ColorConsoleSponge.java | 7 +- .../sponge/ColorPluginAppender.java | 126 ++------------ src/main/resources/config.yml | 5 + 11 files changed, 288 insertions(+), 378 deletions(-) create mode 100644 src/main/java/com/github/games647/colorconsole/common/ColorAppender.java create mode 100644 src/main/java/com/github/games647/colorconsole/common/CommonFormatter.java diff --git a/pom.xml b/pom.xml index f489f77..2069012 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ jar ColorConsole - 1.9 + 2.0 2016 http://dev.bukkit.org/bukkit-plugins/colorconsole/ diff --git a/src/main/java/com/github/games647/colorconsole/bukkit/ColorConsoleBukkit.java b/src/main/java/com/github/games647/colorconsole/bukkit/ColorConsoleBukkit.java index ec489b3..10d7c58 100644 --- a/src/main/java/com/github/games647/colorconsole/bukkit/ColorConsoleBukkit.java +++ b/src/main/java/com/github/games647/colorconsole/bukkit/ColorConsoleBukkit.java @@ -1,8 +1,11 @@ package com.github.games647.colorconsole.bukkit; +import com.google.common.collect.Maps; + import java.io.Serializable; import java.lang.reflect.Field; import java.nio.charset.Charset; +import java.util.Map; import java.util.logging.Level; import org.apache.logging.log4j.LogManager; @@ -30,8 +33,6 @@ public class ColorConsoleBukkit extends JavaPlugin { @Override public void onEnable() { saveDefaultConfig(); - - installLogFormat(); } @Override @@ -78,7 +79,17 @@ public class ColorConsoleBukkit extends JavaPlugin { if (getConfig().getBoolean("colorPluginTag")) { Logger rootLogger = ((Logger) LogManager.getRootLogger()); - ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, this); + ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig()); + Map colors = Maps.newHashMap(); + for (Map.Entry entry : getConfig().getValues(false).entrySet()) { + if (!entry.getKey().startsWith("P-")) { + continue; + } + + colors.put(entry.getKey().replace("P-", ""), (String) entry.getValue()); + } + + pluginAppender.initPluginColors(colors, getConfig().getString("PLUGIN")); pluginAppender.start(); rootLogger.removeAppender(terminalAppender); diff --git a/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java b/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java index 02166ac..074afc2 100644 --- a/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java +++ b/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java @@ -1,158 +1,35 @@ package com.github.games647.colorconsole.bukkit; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSet.Builder; +import com.github.games647.colorconsole.common.ColorAppender; -import java.util.Set; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.SimpleMessage; import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Attribute; -import org.fusesource.jansi.AnsiRenderer; -import org.fusesource.jansi.AnsiRenderer.Code; +import org.bukkit.configuration.file.FileConfiguration; -public class ColorPluginAppender extends AbstractAppender { +public class ColorPluginAppender extends ColorAppender { - private final Appender oldAppender; - - private final ColorConsoleBukkit plugin; - - private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); - private final String defaultPluginColor; - - private final Set pluginNames; - private final Set ignoreMessages; - - public ColorPluginAppender(Appender oldAppender, ColorConsoleBukkit plugin) { - super(oldAppender.getName(), null, oldAppender.getLayout()); - - this.plugin = plugin; - - this.oldAppender = oldAppender; - this.defaultPluginColor = format(plugin.getConfig().getString("PLUGIN")); - this.pluginNames = loadPluginNames(); - this.ignoreMessages = ImmutableSet.copyOf(plugin.getConfig().getStringList("hide-messages")); + public ColorPluginAppender(Appender oldAppender, FileConfiguration config) { + super(oldAppender, config.getStringList("hide-messages"), config.getBoolean("colorPluginTag")); } @Override - public void append(LogEvent logEvent) { - if (oldAppender.isStarted()) { - String oldMessage = logEvent.getMessage().getFormattedMessage(); - for (String ignore : ignoreMessages) { - if (oldMessage.contains(ignore)) { - return; - } - } - - Message newMessage = new SimpleMessage(colorizePluginTag(oldMessage, logEvent.getLevel().name())); - - LogEvent newEvent = new Log4jLogEvent(logEvent.getLoggerName(), logEvent.getMarker(), logEvent.getFQCN() - , logEvent.getLevel(), newMessage, logEvent.getThrown() - , logEvent.getContextMap(), logEvent.getContextStack() - , logEvent.getThreadName(), logEvent.getSource(), logEvent.getMillis()); - oldAppender.append(newEvent); - } + public LogEvent onAppend(LogEvent logEvent) { + String oldMessage = logEvent.getMessage().getFormattedMessage(); + Message newMessage = new SimpleMessage(formatter.colorizePluginTag(oldMessage)); + return clone(logEvent, logEvent.getLoggerName(), newMessage); } - public Appender getOldAppender() { - return oldAppender; - } - - private String colorizePluginTag(String message, String level) { - if (!message.contains("[") || !message.contains("]")) { - return message; - } - - String levelColor = ""; - if (plugin.getConfig().getBoolean("colorLoggingLevel")) { - levelColor = format(plugin.getConfig().getString(level)); - } - - int startTag = message.indexOf('[') + 1; - int endTag = message.indexOf(']', startTag); - - String pluginName = message.substring(startTag, endTag); - if (!pluginNames.contains(pluginName)) { - //it's not a plugin tag -> cancel - return message; - } - - String pluginColor = plugin.getConfig().getString("P-" + pluginName); - if (pluginColor == null) { - pluginColor = defaultPluginColor; - } else { - pluginColor = format(pluginColor); - } - - return reset + '[' + pluginColor + pluginName + reset + ']' + levelColor + message.substring(endTag + 1) + reset; - } - - private String format(String pluginFormat) { - String[] formatParts = pluginFormat.split(" "); - Ansi ansi = Ansi.ansi(); - for (String format : formatParts) { - for (Code ansiCode : AnsiRenderer.Code.values()) { - if (ansiCode.name().equalsIgnoreCase(format)) { - if (ansiCode.isAttribute()) { - ansi.a(ansiCode.getAttribute()); - } else if (ansiCode.isBackground()) { - ansi.bg(ansiCode.getColor()); - } else { - ansi.fg(ansiCode.getColor()); - } - } - } - - if ("blink".equalsIgnoreCase(format)) { - ansi.a(Attribute.BLINK_SLOW); - continue; - } - - if ("strikethrough".equalsIgnoreCase(format)) { - ansi.a(Attribute.STRIKETHROUGH_ON); - continue; - } - - if ("hidden".equalsIgnoreCase(format)) { - ansi.a(Attribute.CONCEAL_OFF); - continue; - } - - if ("dim".equalsIgnoreCase(format)) { - ansi.a(Attribute.INTENSITY_FAINT); - continue; - } - - if ("reverse".equalsIgnoreCase(format)) { - ansi.a(Attribute.NEGATIVE_ON); - continue; - } - - for (Ansi.Color color : Ansi.Color.values()) { - if (format.equalsIgnoreCase(color.name())) { - ansi.fg(color); - break; - } - } - } - - return ansi.toString(); - } - - private Set loadPluginNames() { - Builder setBuilder = ImmutableSet.builder(); - for (Plugin bukkitPlugin : Bukkit.getPluginManager().getPlugins()) { - String loggerName = bukkitPlugin.getDescription().getName(); - setBuilder.add(loggerName); - } - - return setBuilder.build(); + @Override + protected Collection loadPluginNames() { + return Stream.of(Bukkit.getPluginManager().getPlugins()) + .map(plugin -> plugin.getName()) + .collect(Collectors.toSet()); } } diff --git a/src/main/java/com/github/games647/colorconsole/bungee/ColorConsoleBungee.java b/src/main/java/com/github/games647/colorconsole/bungee/ColorConsoleBungee.java index 5da3cc7..ed651e4 100644 --- a/src/main/java/com/github/games647/colorconsole/bungee/ColorConsoleBungee.java +++ b/src/main/java/com/github/games647/colorconsole/bungee/ColorConsoleBungee.java @@ -1,12 +1,5 @@ package com.github.games647.colorconsole.bungee; -import net.md_5.bungee.BungeeCord; -import net.md_5.bungee.api.plugin.Plugin; -import net.md_5.bungee.config.Configuration; -import net.md_5.bungee.config.ConfigurationProvider; -import net.md_5.bungee.config.YamlConfiguration; -import net.md_5.bungee.log.ColouredWriter; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -17,6 +10,13 @@ import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; +import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; +import net.md_5.bungee.log.ColouredWriter; + public class ColorConsoleBungee extends Plugin { private Configuration configuration; @@ -66,7 +66,10 @@ public class ColorConsoleBungee extends Plugin { for (Handler handler : handlers) { if (handler instanceof ColouredWriter) { Formatter oldFormatter = handler.getFormatter(); - handler.setFormatter(new ColorLogFormatter(this, oldFormatter)); + + ColorLogFormatter newFormatter = new ColorLogFormatter(this, oldFormatter); + newFormatter.initPluginColors(getConfiguration().getString("PLUGIN")); + handler.setFormatter(newFormatter); } } } diff --git a/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java b/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java index b2501ad..384c6a1 100644 --- a/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java +++ b/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java @@ -1,24 +1,21 @@ package com.github.games647.colorconsole.bungee; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSet.Builder; +import com.github.games647.colorconsole.common.CommonFormatter; import java.io.PrintWriter; import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; +import java.util.stream.Collectors; import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.plugin.Plugin; - -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Attribute; -import org.fusesource.jansi.AnsiRenderer; -import org.fusesource.jansi.AnsiRenderer.Code; public class ColorLogFormatter extends Formatter { @@ -27,36 +24,30 @@ public class ColorLogFormatter extends Formatter { private final DateFormat date = new SimpleDateFormat("HH:mm:ss"); - private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); - private final String defaultPluginColor; - - private final Set pluginNames; - private final Set ignoreMessages; + private final CommonFormatter formatter; public ColorLogFormatter(ColorConsoleBungee plugin, Formatter oldFormatter) { this.plugin = plugin; this.oldFormatter = oldFormatter; - this.defaultPluginColor = format(plugin.getConfiguration().getString("PLUGIN")); - this.pluginNames = loadPluginNames(); - this.ignoreMessages = ImmutableSet.copyOf(plugin.getConfiguration().getStringList("hide-messages")); + List ignoreMessages = plugin.getConfiguration().getStringList("hide-messages"); + boolean colorizeTag = plugin.getConfiguration().getBoolean("colorPluginTag"); + this.formatter = new CommonFormatter(ignoreMessages, colorizeTag); } @Override public String format(LogRecord record) { - StringBuilder formatted = new StringBuilder(); - - String message = oldFormatter.formatMessage(record); - for (String ignore : ignoreMessages) { - if (message.contains(ignore)) { - return ""; - } + if (formatter.shouldIgnore(record.getMessage())) { + return ""; } + StringBuilder formatted = new StringBuilder(); + String message = oldFormatter.formatMessage(record); + String levelColor = ""; if (plugin.getConfiguration().getBoolean("colorLoggingLevel")) { String log4JName = translateToLog4JName(record.getLevel()); - levelColor = format(plugin.getConfiguration().getString(log4JName)); + levelColor = formatter.format(plugin.getConfiguration().getString(log4JName)); } formatted.append(levelColor); @@ -66,13 +57,9 @@ public class ColorLogFormatter extends Formatter { formatted.append(record.getLevel().getName()); formatted.append("] "); - formatted.append(Ansi.ansi().reset().toString()); + formatted.append(formatter.getReset()); - if (plugin.getConfiguration().getBoolean("colorPluginTag")) { - message = colorizePluginTag(message, levelColor); - } - - formatted.append(message); + formatted.append(formatter.colorizePluginTag(message)); formatted.append('\n'); if (record.getThrown() != null) { @@ -88,82 +75,6 @@ public class ColorLogFormatter extends Formatter { return oldFormatter; } - private String colorizePluginTag(String message, String levelColor) { - if (!message.contains("[") || !message.contains("]")) { - return message; - } - - int startTag = message.indexOf('[') + 1; - int endTag = message.indexOf(']', startTag); - - String pluginName = message.substring(startTag, endTag); - if (!pluginNames.contains(pluginName)) { - //it's not a plugin tag -> cancel - return message; - } - - String pluginColor = plugin.getConfiguration().getString("P-" + pluginName); - if (pluginColor == null || pluginColor.isEmpty()) { - pluginColor = defaultPluginColor; - } else { - pluginColor = format(pluginColor); - } - - return reset + '[' + pluginColor + pluginName + reset + ']' + levelColor + message.substring(endTag + 1); - } - - private String format(String pluginFormat) { - String[] formatParts = pluginFormat.split(" "); - Ansi ansi = Ansi.ansi(); - for (String format : formatParts) { - for (Code ansiCode : AnsiRenderer.Code.values()) { - if (ansiCode.name().equalsIgnoreCase(format)) { - if (ansiCode.isAttribute()) { - ansi.a(ansiCode.getAttribute()); - } else if (ansiCode.isBackground()) { - ansi.bg(ansiCode.getColor()); - } else { - ansi.fg(ansiCode.getColor()); - } - } - } - - if ("blink".equalsIgnoreCase(format)) { - ansi.a(Attribute.BLINK_SLOW); - continue; - } - - if ("strikethrough".equalsIgnoreCase(format)) { - ansi.a(Attribute.STRIKETHROUGH_ON); - continue; - } - - if ("hidden".equalsIgnoreCase(format)) { - ansi.a(Attribute.CONCEAL_OFF); - continue; - } - - if ("dim".equalsIgnoreCase(format)) { - ansi.a(Attribute.INTENSITY_FAINT); - continue; - } - - if ("reverse".equalsIgnoreCase(format)) { - ansi.a(Attribute.NEGATIVE_ON); - continue; - } - - for (Ansi.Color color : Ansi.Color.values()) { - if (format.equalsIgnoreCase(color.name())) { - ansi.fg(color); - break; - } - } - } - - return ansi.toString(); - } - private String translateToLog4JName(Level level) { if (level == Level.SEVERE) { return "ERROR"; @@ -179,12 +90,23 @@ public class ColorLogFormatter extends Formatter { } private Set loadPluginNames() { - Builder setBuilder = ImmutableSet.builder(); - for (Plugin bungeePlugin : ProxyServer.getInstance().getPluginManager().getPlugins()) { - String loggerName = bungeePlugin.getDescription().getName(); - setBuilder.add(loggerName); + return ProxyServer.getInstance().getPluginManager().getPlugins().stream() + .map(plugin -> plugin.getDescription().getName()) + .collect(Collectors.toSet()); + } + + public void initPluginColors(String def) { + Set plugins = loadPluginNames(); + Map pluginColors = new HashMap<>(); + for (String pluginName : plugins) { + String color = plugin.getConfiguration().getString("P-" + pluginName); + if (color == null) { + continue; + } + + pluginColors.put(pluginName, color); } - return setBuilder.build(); + formatter.initPluginColors(plugins, pluginColors, def); } } diff --git a/src/main/java/com/github/games647/colorconsole/common/ColorAppender.java b/src/main/java/com/github/games647/colorconsole/common/ColorAppender.java new file mode 100644 index 0000000..46450b4 --- /dev/null +++ b/src/main/java/com/github/games647/colorconsole/common/ColorAppender.java @@ -0,0 +1,55 @@ +package com.github.games647.colorconsole.common; + +import java.util.Collection; +import java.util.Map; + +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.message.Message; + +public abstract class ColorAppender extends AbstractAppender { + + private final Appender oldAppender; + + protected final CommonFormatter formatter; + + protected ColorAppender(Appender oldAppender, Collection hideMessages, boolean colorizeTag) { + super(oldAppender.getName(), null, oldAppender.getLayout()); + + this.oldAppender = oldAppender; + this.formatter = new CommonFormatter(hideMessages, colorizeTag); + } + + public void initPluginColors(Map configColors, String def) { + formatter.initPluginColors(loadPluginNames(), configColors, def); + } + + @Override + public final void append(LogEvent logEvent) { + if (oldAppender.isStarted()) { + String oldMessage = logEvent.getMessage().getFormattedMessage(); + if (formatter.shouldIgnore(oldMessage)) { + return; + } + + oldAppender.append(onAppend(logEvent)); + } + } + + protected abstract LogEvent onAppend(LogEvent logEvent); + + protected abstract Collection loadPluginNames(); + + protected LogEvent clone(LogEvent oldEvent, String loggerName, Message message) { + return new Log4jLogEvent(loggerName, oldEvent.getMarker(), oldEvent.getFQCN() + , oldEvent.getLevel(), message, oldEvent.getThrown() + , oldEvent.getContextMap(), oldEvent.getContextStack() + , oldEvent.getThreadName(), oldEvent.getSource(), oldEvent.getMillis()); + } + + public Appender getOldAppender() { + return oldAppender; + } +} diff --git a/src/main/java/com/github/games647/colorconsole/common/CommonFormatter.java b/src/main/java/com/github/games647/colorconsole/common/CommonFormatter.java new file mode 100644 index 0000000..fd18bfa --- /dev/null +++ b/src/main/java/com/github/games647/colorconsole/common/CommonFormatter.java @@ -0,0 +1,132 @@ +package com.github.games647.colorconsole.common; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; + +import java.util.Collection; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Attribute; +import org.fusesource.jansi.AnsiRenderer; + +public class CommonFormatter { + + private final String reset = Ansi.ansi().a(Ansi.Attribute.RESET).toString(); + + private Map pluginColors; + private final Set ignoreMessages; + private final boolean colorizeTag; + + public CommonFormatter(Collection ignoreMessages, boolean colorizeTag) { + this.ignoreMessages = ImmutableSet.copyOf(ignoreMessages); + this.colorizeTag = colorizeTag; + } + + public boolean shouldIgnore(String message) { + for (String ignore : ignoreMessages) { + if (message.contains(ignore)) { + return true; + } + } + + return false; + } + + public void initPluginColors(Collection plugins, Map configColors, String def) { + Random random = new Random(); + Ansi.Color[] colors = Ansi.Color.values(); + + ImmutableMap.Builder colorBuilder = ImmutableMap.builder(); + for (String plugin : plugins) { + String styleCode = configColors.getOrDefault(plugin, def); + if (styleCode.equalsIgnoreCase("random")) { + //ignore default + styleCode = colors[random.nextInt(colors.length - 1)].name(); + } + + colorBuilder.put(plugin, format(styleCode)); + } + + this.pluginColors = colorBuilder.build(); + } + + public String colorizePluginTag(String message) { + if (!message.contains("[") || !message.contains("]")) { + return message; + } + + int startTag = message.indexOf('[') + 1; + int endTag = message.indexOf(']', startTag); + + String pluginName = colorizePluginName(message.substring(startTag, endTag)); + return '[' + pluginName + ']' + message.substring(endTag + 1); + } + + public String colorizePluginName(String pluginName) { + if (!colorizeTag) { + return pluginName; + } + + String pluginColor = pluginColors.getOrDefault(pluginName, ""); + return pluginColor + pluginName + reset; + } + + public String format(String pluginFormat) { + String[] formatParts = pluginFormat.split(" "); + Ansi ansi = Ansi.ansi(); + for (String format : formatParts) { + for (AnsiRenderer.Code ansiCode : AnsiRenderer.Code.values()) { + if (ansiCode.name().equalsIgnoreCase(format)) { + if (ansiCode.isAttribute()) { + ansi.a(ansiCode.getAttribute()); + } else if (ansiCode.isBackground()) { + ansi.bg(ansiCode.getColor()); + } else { + ansi.fg(ansiCode.getColor()); + } + } + } + + if ("blink".equalsIgnoreCase(format)) { + ansi.a(Attribute.BLINK_SLOW); + continue; + } + + if ("strikethrough".equalsIgnoreCase(format)) { + ansi.a(Attribute.STRIKETHROUGH_ON); + continue; + } + + if ("hidden".equalsIgnoreCase(format)) { + ansi.a(Attribute.CONCEAL_OFF); + continue; + } + + if ("dim".equalsIgnoreCase(format)) { + ansi.a(Attribute.INTENSITY_FAINT); + continue; + } + + if ("reverse".equalsIgnoreCase(format)) { + ansi.a(Attribute.NEGATIVE_ON); + continue; + } + + for (Ansi.Color color : Ansi.Color.values()) { + if (format.equalsIgnoreCase(color.name())) { + ansi.fg(color); + break; + } + } + } + + return ansi.toString(); + } + + public String getReset() { + return reset; + } +} diff --git a/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleConfig.java b/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleConfig.java index 4286d2c..6fd9ab6 100644 --- a/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleConfig.java +++ b/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleConfig.java @@ -50,7 +50,7 @@ public class ColorConsoleConfig { @Setting(comment = "Log Level Colors") private Map levelColors; - @Setting(comment = "Plugin Colors") + @Setting(comment = "Plugin Colors or random") private String defaultPluginColor = "blue"; @Setting(comment = "Custom plugin colors") diff --git a/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleSponge.java b/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleSponge.java index 9664f67..baa077a 100644 --- a/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleSponge.java +++ b/src/main/java/com/github/games647/colorconsole/sponge/ColorConsoleSponge.java @@ -54,10 +54,6 @@ public class ColorConsoleSponge { return configMapper.getInstance(); } - public CommentedConfigurationNode getConfigRaw() { - return rootNode; - } - @Listener //During this state, the plugin gets ready for initialization. Logger and config public void onPreInit(GamePreInitializationEvent preInitEvent) { logger.info("Setting up config"); @@ -106,7 +102,8 @@ public class ColorConsoleSponge { org.apache.logging.log4j.core.Logger rootLogger = ((org.apache.logging.log4j.core.Logger) LogManager .getRootLogger()); - ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, this); + ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig()); + pluginAppender.initPluginColors(getConfig().getPluginColors(), getConfig().getDefaultPluginColor()); pluginAppender.start(); rootLogger.removeAppender(terminalAppender); diff --git a/src/main/java/com/github/games647/colorconsole/sponge/ColorPluginAppender.java b/src/main/java/com/github/games647/colorconsole/sponge/ColorPluginAppender.java index d14b663..b12d2c6 100644 --- a/src/main/java/com/github/games647/colorconsole/sponge/ColorPluginAppender.java +++ b/src/main/java/com/github/games647/colorconsole/sponge/ColorPluginAppender.java @@ -1,122 +1,30 @@ package com.github.games647.colorconsole.sponge; -import com.google.common.collect.ImmutableSet; -import java.util.Set; +import com.github.games647.colorconsole.common.ColorAppender; + +import java.util.Collection; +import java.util.stream.Collectors; + import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Attribute; -import org.fusesource.jansi.AnsiRenderer; -import org.fusesource.jansi.AnsiRenderer.Code; +import org.spongepowered.api.Sponge; -public class ColorPluginAppender extends AbstractAppender { +public class ColorPluginAppender extends ColorAppender { - private final Appender oldAppender; - - private final ColorConsoleSponge plugin; - - private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); - private final String defaultPluginColor; - - private final Set ignoreMessages; - - public ColorPluginAppender(Appender oldAppender, ColorConsoleSponge plugin) { - super(oldAppender.getName(), null, oldAppender.getLayout()); - - this.plugin = plugin; - - this.oldAppender = oldAppender; - this.defaultPluginColor = format(plugin.getConfig().getDefaultPluginColor()); - this.ignoreMessages = ImmutableSet.copyOf(plugin.getConfig().getHideMessages()); + public ColorPluginAppender(Appender oldAppender, ColorConsoleConfig config) { + super(oldAppender, config.getHideMessages(), config.isColorPluginTag()); } @Override - public void append(LogEvent logEvent) { - if (oldAppender.isStarted()) { - String oldMessage = logEvent.getMessage().getFormattedMessage(); - for (String ignore : ignoreMessages) { - if (oldMessage.contains(ignore)) { - return; - } - } - - String loggerName = logEvent.getLoggerName(); - String pluginColor = plugin.getConfig().getPluginColors().get(loggerName); - if (pluginColor == null) { - pluginColor = defaultPluginColor; - } else { - pluginColor = format(pluginColor); - } - - String levelColor = ""; - if (plugin.getConfig().isColorLoggingLevel()) { - levelColor = format(plugin.getConfig().getLevelColors().get(logEvent.getLevel().name())); - } - - String newLoggerName = pluginColor + loggerName + reset + levelColor; - - LogEvent newEvent = new Log4jLogEvent(newLoggerName, logEvent.getMarker(), logEvent.getFQCN() - , logEvent.getLevel(), logEvent.getMessage(), logEvent.getThrown(), logEvent.getContextMap() - , logEvent.getContextStack(), logEvent.getThreadName(), logEvent.getSource(), logEvent.getMillis()); - oldAppender.append(newEvent); - } + public LogEvent onAppend(LogEvent logEvent) { + String newLoggerName = formatter.colorizePluginName(logEvent.getLoggerName()); + return clone(logEvent, newLoggerName, logEvent.getMessage()); } - public Appender getOldAppender() { - return oldAppender; - } - - private String format(String pluginFormat) { - String[] formatParts = pluginFormat.split(" "); - Ansi ansi = Ansi.ansi(); - for (String format : formatParts) { - for (Code ansiCode : AnsiRenderer.Code.values()) { - if (ansiCode.name().equalsIgnoreCase(format)) { - if (ansiCode.isAttribute()) { - ansi.a(ansiCode.getAttribute()); - } else if (ansiCode.isBackground()) { - ansi.bg(ansiCode.getColor()); - } else { - ansi.fg(ansiCode.getColor()); - } - } - } - - if ("blink".equalsIgnoreCase(format)) { - ansi.a(Attribute.BLINK_SLOW); - continue; - } - - if ("strikethrough".equalsIgnoreCase(format)) { - ansi.a(Attribute.STRIKETHROUGH_ON); - continue; - } - - if ("hidden".equalsIgnoreCase(format)) { - ansi.a(Attribute.CONCEAL_OFF); - continue; - } - - if ("dim".equalsIgnoreCase(format)) { - ansi.a(Attribute.INTENSITY_FAINT); - continue; - } - - if ("reverse".equalsIgnoreCase(format)) { - ansi.a(Attribute.NEGATIVE_ON); - continue; - } - - for (Ansi.Color color : Ansi.Color.values()) { - if (format.equalsIgnoreCase(color.name())) { - ansi.fg(color); - break; - } - } - } - - return ansi.toString(); + @Override + protected Collection loadPluginNames() { + return Sponge.getPluginManager().getPlugins().stream() + .map(pluginContainer -> pluginContainer.getId()) + .collect(Collectors.toSet()); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6c6b3b1..50b6bec 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -30,7 +30,12 @@ DEBUG: green bold TRACE: blue # Plugin Colors + +# This can be the default color or "random" it gives each plugin (besides the ones specified below) a different color +# which keeps the same until the server shuts down. PLUGIN: blue +# Plugin: random + P-Essentials: green P-LagMonitor: red P-WorldEdit: red