Colorize only available plugin names

Do not color brackets when there other content in it
This commit is contained in:
games647 2016-06-11 12:07:40 +02:00
parent ac63c37b46
commit b17d0ec4bd
4 changed files with 58 additions and 4 deletions

View File

@ -8,7 +8,7 @@
<packaging>jar</packaging>
<name>ColorConsole</name>
<version>1.6</version>
<version>1.7</version>
<inceptionYear>2016</inceptionYear>
<url>http://dev.bukkit.org/bukkit-plugins/colorconsole/</url>
<description>

View File

@ -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<String> 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<String> loadPluginNames() {
Builder<String> setBuilder = ImmutableSet.builder();
for (Plugin bukkitPlugin : Bukkit.getPluginManager().getPlugins()) {
String loggerName = bukkitPlugin.getDescription().getName();
setBuilder.add(loggerName);
}
return setBuilder.build();
}
}

View File

@ -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<String> 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<String> loadPluginNames() {
Builder<String> setBuilder = ImmutableSet.builder();
for (Plugin bungeePlugin : ProxyServer.getInstance().getPluginManager().getPlugins()) {
String loggerName = bungeePlugin.getDescription().getName();
setBuilder.add(loggerName);
}
return setBuilder.build();
}
}

View File

@ -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