Add configurable Plugin colors

Also add per-plugin color highlighing
This commit is contained in:
Vinnie 2016-05-06 06:11:14 -04:00
parent f44a6f7af2
commit abf5622003
3 changed files with 51 additions and 7 deletions

View File

@ -77,7 +77,7 @@ public class ColorConsole extends JavaPlugin {
if (getConfig().getBoolean("colorPluginTag")) {
Logger rootLogger = ((Logger) LogManager.getRootLogger());
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender);
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, this);
pluginAppender.start();
rootLogger.removeAppender(terminalAppender);

View File

@ -12,17 +12,23 @@ import org.fusesource.jansi.Ansi.Attribute;
public class ColorPluginAppender extends AbstractAppender {
private final Appender oldAppender;
private final ColorConsole plugin;
private final String pluginColor;
private final String reset;
public ColorPluginAppender(Appender oldAppender) {
public ColorPluginAppender(Appender oldAppender, ColorConsole plugin) {
super(oldAppender.getName(), null, oldAppender.getLayout());
this.oldAppender = oldAppender;
this.plugin = plugin;
this.reset = Ansi.ansi().a(Attribute.RESET).toString();
this.pluginColor = format(plugin.getConfig().getString("PLUGIN"));
}
@Override
public void append(LogEvent event) {
if (isStarted() && oldAppender.isStarted()) {
Message newMessage = new SimpleMessage(colorizePluginTag(event.getMessage().getFormattedMessage()));
Message newMessage = new SimpleMessage(colorizePluginTag(event.getMessage().getFormattedMessage(), event.getLevel().name()));
LogEvent newEvent = new Log4jLogEvent(event.getLoggerName(), event.getMarker(), event.getFQCN()
, event.getLevel(), newMessage, event.getThrown(), event.getContextMap()
@ -35,15 +41,47 @@ public class ColorPluginAppender extends AbstractAppender {
return oldAppender;
}
private String colorizePluginTag(String message) {
private String colorizePluginTag(String message, String level) {
if (!message.contains("[") || !message.contains("]")) {
return message;
}
String levelColor = format(plugin.getConfig().getString(level));
int start = message.indexOf('[') + 1;
int end = message.indexOf(']', start);
String prefix = Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).toString();
String resetSuffix = Ansi.ansi().a(Attribute.RESET).toString();
return '[' + prefix + message.substring(start, end) + resetSuffix + message.substring(end);
String pluginName = message.substring(start, end);
String thisPluginColor = plugin.getConfig().getString("P-" + pluginName);
if (thisPluginColor != null) {
thisPluginColor = format(thisPluginColor);
} else {
thisPluginColor = pluginColor;
}
return reset + '[' + thisPluginColor + pluginName + reset + ']' + levelColor + message.substring(end + 1);
}
private String format(String pluginFormat) {
String[] formatParts = pluginFormat.split(" ");
Ansi ansi = Ansi.ansi();
for (String format : formatParts) {
if (format.equalsIgnoreCase("blink")) {
ansi.a(Attribute.BLINK_SLOW);
continue;
}
if (format.equalsIgnoreCase("bold")) {
ansi.a(Attribute.INTENSITY_BOLD);
continue;
}
if (format.equalsIgnoreCase("underline")) {
ansi.a(Attribute.UNDERLINE);
continue;
}
for (Ansi.Color color : Ansi.Color.values()) {
if (format.equalsIgnoreCase(color.name())) {
ansi.fg(color);
}
}
}
return ansi.toString();
}
}

View File

@ -29,3 +29,9 @@ INFO: gray
DEBUG: green bold
TRACE: blue
#Plugin Colors
PLUGIN: blue
P-Essentials: green
P-WorldEdit: red
P-WorldGuard: cyan
P-Vault: magenta