From b17d0ec4bda16bbe4eb7da8e2b7377f56633a33d Mon Sep 17 00:00:00 2001 From: games647 Date: Sat, 11 Jun 2016 12:07:40 +0200 Subject: [PATCH] Colorize only available plugin names Do not color brackets when there other content in it --- pom.xml | 2 +- .../bukkit/ColorPluginAppender.java | 29 +++++++++++++++++-- .../bungee/ColorLogFormatter.java | 26 ++++++++++++++++- src/main/resources/config.yml | 5 ++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index adfaea1..5c4a253 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ jar ColorConsole - 1.6 + 1.7 2016 http://dev.bukkit.org/bukkit-plugins/colorconsole/ 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 2b4d9fb..985847c 100644 --- a/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java +++ b/src/main/java/com/github/games647/colorconsole/bukkit/ColorPluginAppender.java @@ -1,11 +1,18 @@ package com.github.games647.colorconsole.bukkit; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSet.Builder; + +import java.util.Set; + 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; @@ -20,6 +27,8 @@ public class ColorPluginAppender extends AbstractAppender { private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); private final String defaultPluginColor; + private final Set pluginNames; + public ColorPluginAppender(Appender oldAppender, ColorConsoleBukkit plugin) { super(oldAppender.getName(), null, oldAppender.getLayout()); @@ -27,6 +36,7 @@ public class ColorPluginAppender extends AbstractAppender { this.oldAppender = oldAppender; this.defaultPluginColor = format(plugin.getConfig().getString("PLUGIN")); + this.pluginNames = loadPluginNames(); } @Override @@ -36,8 +46,8 @@ public class ColorPluginAppender extends AbstractAppender { , event.getLevel().name())); LogEvent newEvent = new Log4jLogEvent(event.getLoggerName(), event.getMarker(), event.getFQCN() - , event.getLevel(), newMessage, event.getThrown(), event.getContextMap() - , event.getContextStack(), event.getThreadName(), event.getSource(), event.getMillis()); + , event.getLevel(), newMessage, event.getThrown(), event.getContextMap(), event.getContextStack() + , event.getThreadName(), event.getSource(), event.getMillis()); oldAppender.append(newEvent); } } @@ -60,6 +70,11 @@ public class ColorPluginAppender extends AbstractAppender { 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; @@ -121,4 +136,14 @@ public class ColorPluginAppender extends AbstractAppender { 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(); + } } 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 6eacd02..5128528 100644 --- a/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java +++ b/src/main/java/com/github/games647/colorconsole/bungee/ColorLogFormatter.java @@ -1,13 +1,20 @@ package com.github.games647.colorconsole.bungee; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSet.Builder; + import java.io.PrintWriter; import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Set; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; +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; @@ -23,18 +30,20 @@ public class ColorLogFormatter extends Formatter { private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); private final String defaultPluginColor; + private final Set pluginNames; + public ColorLogFormatter(ColorConsoleBungee plugin, Formatter oldFormatter) { this.plugin = plugin; this.oldFormatter = oldFormatter; this.defaultPluginColor = format(plugin.getConfiguration().getString("PLUGIN")); + this.pluginNames = loadPluginNames(); } @Override public String format(LogRecord record) { StringBuilder formatted = new StringBuilder(); - String levelName = record.getLevel().getName(); String levelColor = ""; if (plugin.getConfiguration().getBoolean("colorLoggingLevel")) { String log4JName = translateToLog4JName(record.getLevel()); @@ -78,6 +87,11 @@ public class ColorLogFormatter extends Formatter { 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; @@ -153,4 +167,14 @@ public class ColorLogFormatter extends Formatter { return "TRACE"; } } + + private Set loadPluginNames() { + Builder setBuilder = ImmutableSet.builder(); + for (Plugin bungeePlugin : ProxyServer.getInstance().getPluginManager().getPlugins()) { + String loggerName = bungeePlugin.getDescription().getName(); + setBuilder.add(loggerName); + } + + return setBuilder.build(); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4d6446f..63775ed 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -32,9 +32,14 @@ TRACE: blue # Plugin Colors PLUGIN: blue P-Essentials: green +P-LagMonitor: red P-WorldEdit: red +P-FastLogin: cyan P-WorldGuard: cyan P-Vault: magenta +P-ChangeSkin: yellow +P-ScoreboardStats: white +P-mcMMOAction: blue # Available foreground colors | Available background colors # Black | BG_Black