mirror of
https://github.com/games647/ColorConsole.git
synced 2024-12-03 15:53:26 +01:00
Refactor formatter to have less duplicate code + Add random plugin color
This commit is contained in:
parent
d3c9aa6184
commit
30fead1dfc
2
pom.xml
2
pom.xml
@ -8,7 +8,7 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ColorConsole</name>
|
||||
<version>1.9</version>
|
||||
<version>2.0</version>
|
||||
<inceptionYear>2016</inceptionYear>
|
||||
<url>http://dev.bukkit.org/bukkit-plugins/colorconsole/</url>
|
||||
<description>
|
||||
|
@ -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<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();
|
||||
|
||||
rootLogger.removeAppender(terminalAppender);
|
||||
|
@ -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<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"));
|
||||
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<String> loadPluginNames() {
|
||||
Builder<String> setBuilder = ImmutableSet.builder();
|
||||
for (Plugin bukkitPlugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
String loggerName = bukkitPlugin.getDescription().getName();
|
||||
setBuilder.add(loggerName);
|
||||
}
|
||||
|
||||
return setBuilder.build();
|
||||
@Override
|
||||
protected Collection<String> loadPluginNames() {
|
||||
return Stream.of(Bukkit.getPluginManager().getPlugins())
|
||||
.map(plugin -> plugin.getName())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<String> pluginNames;
|
||||
private final Set<String> 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<String> 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<String> loadPluginNames() {
|
||||
Builder<String> 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<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);
|
||||
}
|
||||
|
||||
return setBuilder.build();
|
||||
formatter.initPluginColors(plugins, pluginColors, def);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ public class ColorConsoleConfig {
|
||||
@Setting(comment = "Log Level Colors")
|
||||
private Map<String, String> levelColors;
|
||||
|
||||
@Setting(comment = "Plugin Colors")
|
||||
@Setting(comment = "Plugin Colors or random")
|
||||
private String defaultPluginColor = "blue";
|
||||
|
||||
@Setting(comment = "Custom plugin colors")
|
||||
|
@ -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);
|
||||
|
@ -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<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());
|
||||
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<String> loadPluginNames() {
|
||||
return Sponge.getPluginManager().getPlugins().stream()
|
||||
.map(pluginContainer -> pluginContainer.getId())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user