Refactor formatter to have less duplicate code + Add random plugin color

This commit is contained in:
games647 2017-05-21 13:07:40 +02:00
parent d3c9aa6184
commit 30fead1dfc
No known key found for this signature in database
GPG Key ID: E9E962F08F621538
11 changed files with 288 additions and 378 deletions

View File

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

View File

@ -1,8 +1,11 @@
package com.github.games647.colorconsole.bukkit; package com.github.games647.colorconsole.bukkit;
import com.google.common.collect.Maps;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -30,8 +33,6 @@ public class ColorConsoleBukkit extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
installLogFormat();
} }
@Override @Override
@ -78,7 +79,17 @@ public class ColorConsoleBukkit extends JavaPlugin {
if (getConfig().getBoolean("colorPluginTag")) { if (getConfig().getBoolean("colorPluginTag")) {
Logger rootLogger = ((Logger) LogManager.getRootLogger()); Logger rootLogger = ((Logger) LogManager.getRootLogger());
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, this); ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig());
Map<String, String> colors = Maps.newHashMap();
for (Map.Entry<String, Object> 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(); pluginAppender.start();
rootLogger.removeAppender(terminalAppender); rootLogger.removeAppender(terminalAppender);

View File

@ -1,158 +1,35 @@
package com.github.games647.colorconsole.bukkit; package com.github.games647.colorconsole.bukkit;
import com.google.common.collect.ImmutableSet; import com.github.games647.colorconsole.common.ColorAppender;
import com.google.common.collect.ImmutableSet.Builder;
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.Appender;
import org.apache.logging.log4j.core.LogEvent; 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.Message;
import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.message.SimpleMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.configuration.file.FileConfiguration;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Attribute;
import org.fusesource.jansi.AnsiRenderer;
import org.fusesource.jansi.AnsiRenderer.Code;
public class ColorPluginAppender extends AbstractAppender { public class ColorPluginAppender extends ColorAppender {
private final Appender oldAppender; public ColorPluginAppender(Appender oldAppender, FileConfiguration config) {
super(oldAppender, config.getStringList("hide-messages"), config.getBoolean("colorPluginTag"));
private final ColorConsoleBukkit plugin;
private final String reset = Ansi.ansi().a(Attribute.RESET).toString();
private final String defaultPluginColor;
private final Set<String> pluginNames;
private final Set<String> 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"));
} }
@Override @Override
public void append(LogEvent logEvent) { public LogEvent onAppend(LogEvent logEvent) {
if (oldAppender.isStarted()) {
String oldMessage = logEvent.getMessage().getFormattedMessage(); String oldMessage = logEvent.getMessage().getFormattedMessage();
for (String ignore : ignoreMessages) { Message newMessage = new SimpleMessage(formatter.colorizePluginTag(oldMessage));
if (oldMessage.contains(ignore)) { return clone(logEvent, logEvent.getLoggerName(), newMessage);
return;
}
} }
Message newMessage = new SimpleMessage(colorizePluginTag(oldMessage, logEvent.getLevel().name())); @Override
protected Collection<String> loadPluginNames() {
LogEvent newEvent = new Log4jLogEvent(logEvent.getLoggerName(), logEvent.getMarker(), logEvent.getFQCN() return Stream.of(Bukkit.getPluginManager().getPlugins())
, logEvent.getLevel(), newMessage, logEvent.getThrown() .map(plugin -> plugin.getName())
, logEvent.getContextMap(), logEvent.getContextStack() .collect(Collectors.toSet());
, logEvent.getThreadName(), logEvent.getSource(), logEvent.getMillis());
oldAppender.append(newEvent);
}
}
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<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,12 +1,5 @@
package com.github.games647.colorconsole.bungee; 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.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -17,6 +10,13 @@ import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; 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 { public class ColorConsoleBungee extends Plugin {
private Configuration configuration; private Configuration configuration;
@ -66,7 +66,10 @@ public class ColorConsoleBungee extends Plugin {
for (Handler handler : handlers) { for (Handler handler : handlers) {
if (handler instanceof ColouredWriter) { if (handler instanceof ColouredWriter) {
Formatter oldFormatter = handler.getFormatter(); Formatter oldFormatter = handler.getFormatter();
handler.setFormatter(new ColorLogFormatter(this, oldFormatter));
ColorLogFormatter newFormatter = new ColorLogFormatter(this, oldFormatter);
newFormatter.initPluginColors(getConfiguration().getString("PLUGIN"));
handler.setFormatter(newFormatter);
} }
} }
} }

View File

@ -1,24 +1,21 @@
package com.github.games647.colorconsole.bungee; package com.github.games647.colorconsole.bungee;
import com.google.common.collect.ImmutableSet; import com.github.games647.colorconsole.common.CommonFormatter;
import com.google.common.collect.ImmutableSet.Builder;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.stream.Collectors;
import net.md_5.bungee.api.ProxyServer; 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 { 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 DateFormat date = new SimpleDateFormat("HH:mm:ss");
private final String reset = Ansi.ansi().a(Attribute.RESET).toString(); private final CommonFormatter formatter;
private final String defaultPluginColor;
private final Set<String> pluginNames;
private final Set<String> ignoreMessages;
public ColorLogFormatter(ColorConsoleBungee plugin, Formatter oldFormatter) { public ColorLogFormatter(ColorConsoleBungee plugin, Formatter oldFormatter) {
this.plugin = plugin; this.plugin = plugin;
this.oldFormatter = oldFormatter; this.oldFormatter = oldFormatter;
this.defaultPluginColor = format(plugin.getConfiguration().getString("PLUGIN")); List<String> ignoreMessages = plugin.getConfiguration().getStringList("hide-messages");
this.pluginNames = loadPluginNames(); boolean colorizeTag = plugin.getConfiguration().getBoolean("colorPluginTag");
this.ignoreMessages = ImmutableSet.copyOf(plugin.getConfiguration().getStringList("hide-messages")); this.formatter = new CommonFormatter(ignoreMessages, colorizeTag);
} }
@Override @Override
public String format(LogRecord record) { public String format(LogRecord record) {
StringBuilder formatted = new StringBuilder(); if (formatter.shouldIgnore(record.getMessage())) {
String message = oldFormatter.formatMessage(record);
for (String ignore : ignoreMessages) {
if (message.contains(ignore)) {
return ""; return "";
} }
}
StringBuilder formatted = new StringBuilder();
String message = oldFormatter.formatMessage(record);
String levelColor = ""; String levelColor = "";
if (plugin.getConfiguration().getBoolean("colorLoggingLevel")) { if (plugin.getConfiguration().getBoolean("colorLoggingLevel")) {
String log4JName = translateToLog4JName(record.getLevel()); String log4JName = translateToLog4JName(record.getLevel());
levelColor = format(plugin.getConfiguration().getString(log4JName)); levelColor = formatter.format(plugin.getConfiguration().getString(log4JName));
} }
formatted.append(levelColor); formatted.append(levelColor);
@ -66,13 +57,9 @@ public class ColorLogFormatter extends Formatter {
formatted.append(record.getLevel().getName()); formatted.append(record.getLevel().getName());
formatted.append("] "); formatted.append("] ");
formatted.append(Ansi.ansi().reset().toString()); formatted.append(formatter.getReset());
if (plugin.getConfiguration().getBoolean("colorPluginTag")) { formatted.append(formatter.colorizePluginTag(message));
message = colorizePluginTag(message, levelColor);
}
formatted.append(message);
formatted.append('\n'); formatted.append('\n');
if (record.getThrown() != null) { if (record.getThrown() != null) {
@ -88,82 +75,6 @@ public class ColorLogFormatter extends Formatter {
return oldFormatter; 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) { private String translateToLog4JName(Level level) {
if (level == Level.SEVERE) { if (level == Level.SEVERE) {
return "ERROR"; return "ERROR";
@ -179,12 +90,23 @@ public class ColorLogFormatter extends Formatter {
} }
private Set<String> loadPluginNames() { private Set<String> loadPluginNames() {
Builder<String> setBuilder = ImmutableSet.builder(); return ProxyServer.getInstance().getPluginManager().getPlugins().stream()
for (Plugin bungeePlugin : ProxyServer.getInstance().getPluginManager().getPlugins()) { .map(plugin -> plugin.getDescription().getName())
String loggerName = bungeePlugin.getDescription().getName(); .collect(Collectors.toSet());
setBuilder.add(loggerName);
} }
return setBuilder.build(); public void initPluginColors(String def) {
Set<String> plugins = loadPluginNames();
Map<String, String> pluginColors = new HashMap<>();
for (String pluginName : plugins) {
String color = plugin.getConfiguration().getString("P-" + pluginName);
if (color == null) {
continue;
}
pluginColors.put(pluginName, color);
}
formatter.initPluginColors(plugins, pluginColors, def);
} }
} }

View File

@ -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<String> hideMessages, boolean colorizeTag) {
super(oldAppender.getName(), null, oldAppender.getLayout());
this.oldAppender = oldAppender;
this.formatter = new CommonFormatter(hideMessages, colorizeTag);
}
public void initPluginColors(Map<String, String> 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<String> 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;
}
}

View File

@ -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<String, String> pluginColors;
private final Set<String> ignoreMessages;
private final boolean colorizeTag;
public CommonFormatter(Collection<String> 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<String> plugins, Map<String, String> configColors, String def) {
Random random = new Random();
Ansi.Color[] colors = Ansi.Color.values();
ImmutableMap.Builder<String, String> 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;
}
}

View File

@ -50,7 +50,7 @@ public class ColorConsoleConfig {
@Setting(comment = "Log Level Colors") @Setting(comment = "Log Level Colors")
private Map<String, String> levelColors; private Map<String, String> levelColors;
@Setting(comment = "Plugin Colors") @Setting(comment = "Plugin Colors or random")
private String defaultPluginColor = "blue"; private String defaultPluginColor = "blue";
@Setting(comment = "Custom plugin colors") @Setting(comment = "Custom plugin colors")

View File

@ -54,10 +54,6 @@ public class ColorConsoleSponge {
return configMapper.getInstance(); return configMapper.getInstance();
} }
public CommentedConfigurationNode getConfigRaw() {
return rootNode;
}
@Listener //During this state, the plugin gets ready for initialization. Logger and config @Listener //During this state, the plugin gets ready for initialization. Logger and config
public void onPreInit(GamePreInitializationEvent preInitEvent) { public void onPreInit(GamePreInitializationEvent preInitEvent) {
logger.info("Setting up config"); 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 org.apache.logging.log4j.core.Logger rootLogger = ((org.apache.logging.log4j.core.Logger) LogManager
.getRootLogger()); .getRootLogger());
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, this); ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig());
pluginAppender.initPluginColors(getConfig().getPluginColors(), getConfig().getDefaultPluginColor());
pluginAppender.start(); pluginAppender.start();
rootLogger.removeAppender(terminalAppender); rootLogger.removeAppender(terminalAppender);

View File

@ -1,122 +1,30 @@
package com.github.games647.colorconsole.sponge; package com.github.games647.colorconsole.sponge;
import com.google.common.collect.ImmutableSet; import com.github.games647.colorconsole.common.ColorAppender;
import java.util.Set;
import java.util.Collection;
import java.util.stream.Collectors;
import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender; import org.spongepowered.api.Sponge;
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;
public class ColorPluginAppender extends AbstractAppender { public class ColorPluginAppender extends ColorAppender {
private final Appender oldAppender; public ColorPluginAppender(Appender oldAppender, ColorConsoleConfig config) {
super(oldAppender, config.getHideMessages(), config.isColorPluginTag());
private final ColorConsoleSponge plugin;
private final String reset = Ansi.ansi().a(Attribute.RESET).toString();
private final String defaultPluginColor;
private final Set<String> 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());
} }
@Override @Override
public void append(LogEvent logEvent) { public LogEvent onAppend(LogEvent logEvent) {
if (oldAppender.isStarted()) { String newLoggerName = formatter.colorizePluginName(logEvent.getLoggerName());
String oldMessage = logEvent.getMessage().getFormattedMessage(); return clone(logEvent, newLoggerName, logEvent.getMessage());
for (String ignore : ignoreMessages) {
if (oldMessage.contains(ignore)) {
return;
}
} }
String loggerName = logEvent.getLoggerName(); @Override
String pluginColor = plugin.getConfig().getPluginColors().get(loggerName); protected Collection<String> loadPluginNames() {
if (pluginColor == null) { return Sponge.getPluginManager().getPlugins().stream()
pluginColor = defaultPluginColor; .map(pluginContainer -> pluginContainer.getId())
} else { .collect(Collectors.toSet());
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 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();
} }
} }

View File

@ -30,7 +30,12 @@ DEBUG: green bold
TRACE: blue TRACE: blue
# Plugin Colors # 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: blue
# Plugin: random
P-Essentials: green P-Essentials: green
P-LagMonitor: red P-LagMonitor: red
P-WorldEdit: red P-WorldEdit: red