Add complete message formatting back

This commit is contained in:
games647 2017-06-02 11:50:31 +02:00
parent 327ca3c614
commit 8c6718ad81
No known key found for this signature in database
GPG Key ID: E9E962F08F621538
8 changed files with 68 additions and 20 deletions

View File

@ -24,8 +24,15 @@ public class ColorConsoleBukkit extends JavaPlugin {
public void onLoad() {
saveDefaultConfig();
Map<String, String> levelColors = Maps.newHashMap();
levelColors.put("FATAL", getConfig().getString("FATAL"));
levelColors.put("ERROR", getConfig().getString("ERROR"));
levelColors.put("WARN", getConfig().getString("WARN"));
levelColors.put("DEBUG", getConfig().getString("DEBUG"));
levelColors.put("TRACE", getConfig().getString("TRACE"));
//try to run it as early as possible
installLogFormat();
installLogFormat(levelColors);
}
@Override
@ -59,7 +66,7 @@ public class ColorConsoleBukkit extends JavaPlugin {
}
}
private void installLogFormat() {
private void installLogFormat(Map<String, String> levelColors) {
Appender terminalAppender = CommonLogInstaller.getTerminalAppender(TERMINAL_NAME);
oldLayout = terminalAppender.getLayout();
@ -84,7 +91,7 @@ public class ColorConsoleBukkit extends JavaPlugin {
getLogger().log(Level.WARNING, "Cannot install log format", ex);
}
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig());
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender, getConfig(), levelColors);
Map<String, String> colors = Maps.newHashMap();
for (Map.Entry<String, Object> entry : getConfig().getValues(false).entrySet()) {
if (!entry.getKey().startsWith("P-")) {

View File

@ -3,6 +3,8 @@ package com.github.games647.colorconsole.bukkit;
import com.github.games647.colorconsole.common.ColorAppender;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -15,17 +17,18 @@ import org.bukkit.configuration.file.FileConfiguration;
public class ColorPluginAppender extends ColorAppender {
public ColorPluginAppender(Appender oldAppender, FileConfiguration config) {
public ColorPluginAppender(Appender oldAppender, FileConfiguration config, Map<String, String> levelColors) {
super(oldAppender
, config.getStringList("hide-messages")
, config.getBoolean("colorPluginTag")
, config.getBoolean("truncateColor"));
, config.getBoolean("truncateColor")
, config.getBoolean("colorMessage") ? levelColors : Collections.emptyMap());
}
@Override
public LogEvent onAppend(LogEvent logEvent) {
String oldMessage = logEvent.getMessage().getFormattedMessage();
Message newMessage = new SimpleMessage(formatter.colorizePluginTag(oldMessage));
Message newMessage = new SimpleMessage(formatter.colorizePluginTag(oldMessage, logEvent.getLevel().name()));
return clone(logEvent, logEvent.getLoggerName(), newMessage);
}

View File

@ -1,6 +1,7 @@
package com.github.games647.colorconsole.bungee;
import com.github.games647.colorconsole.common.CommonFormatter;
import com.google.common.collect.Maps;
import java.io.PrintWriter;
import java.io.StringWriter;
@ -33,7 +34,17 @@ public class ColorLogFormatter extends Formatter {
List<String> ignoreMessages = plugin.getConfiguration().getStringList("hide-messages");
boolean colorizeTag = plugin.getConfiguration().getBoolean("colorPluginTag");
boolean truncateColor = plugin.getConfiguration().getBoolean("truncateColor", false);
this.formatter = new CommonFormatter(ignoreMessages, colorizeTag, truncateColor);
Map<String, String> levelColors = Maps.newHashMap();
if (plugin.getConfiguration().getBoolean("colorMessage", false)) {
levelColors.put("FATAL", plugin.getConfiguration().getString("FATAL"));
levelColors.put("ERROR", plugin.getConfiguration().getString("ERROR"));
levelColors.put("WARN", plugin.getConfiguration().getString("WARN"));
levelColors.put("DEBUG", plugin.getConfiguration().getString("DEBUG"));
levelColors.put("TRACE", plugin.getConfiguration().getString("TRACE"));
}
this.formatter = new CommonFormatter(ignoreMessages, colorizeTag, truncateColor, levelColors);
}
@Override
@ -60,7 +71,7 @@ public class ColorLogFormatter extends Formatter {
formatted.append(formatter.getReset());
formatted.append(formatter.colorizePluginTag(message));
formatted.append(formatter.colorizePluginTag(message, translateToLog4JName(record.getLevel())));
formatted.append('\n');
if (record.getThrown() != null) {

View File

@ -18,11 +18,11 @@ public abstract class ColorAppender extends AbstractAppender {
protected final CommonFormatter formatter;
protected ColorAppender(Appender oldAppender, Collection<String> hideMessages
, boolean colorizeTag, boolean truncateColor) {
, boolean colorizeTag, boolean truncateColor, Map<String, String> levelColors) {
super(oldAppender.getName(), null, oldAppender.getLayout());
this.oldAppender = oldAppender;
this.formatter = new CommonFormatter(hideMessages, colorizeTag, truncateColor);
this.formatter = new CommonFormatter(hideMessages, colorizeTag, truncateColor, levelColors);
for (Method method : LogEvent.class.getDeclaredMethods()) {
String methodName = method.getName();

View File

@ -1,6 +1,7 @@
package com.github.games647.colorconsole.common;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
@ -25,11 +26,25 @@ public class CommonFormatter {
private final boolean colorizeTag;
private final boolean truncateColor;
private Map<String, String> pluginColors;
private Map<String, String> levelColors;
public CommonFormatter(Collection<String> ignoreMessages, boolean colorizeTag, boolean truncateColor) {
public CommonFormatter(Collection<String> ignoreMessages, boolean colorizeTag, boolean truncateColor
, Map<String, String> levelColors) {
this.ignoreMessages = ImmutableSet.copyOf(ignoreMessages);
this.colorizeTag = colorizeTag;
this.truncateColor = truncateColor;
Builder<String, String> builder = ImmutableMap.builder();
for (Map.Entry<String, String> entry : levelColors.entrySet()) {
if (entry.getKey().equals("INFO")) {
continue;
}
builder.put(entry.getKey(), format(entry.getValue()));
}
this.levelColors = builder.build();
}
public boolean shouldIgnore(String message) {
@ -62,9 +77,9 @@ public class CommonFormatter {
this.pluginColors = colorBuilder.build();
}
public String colorizePluginTag(String message) {
public String colorizePluginTag(String message, String level) {
if (!message.contains("[") || !message.contains("]")) {
return message;
return levelColors.getOrDefault(level, "") + message + reset;
}
String newMessage = message;
@ -83,7 +98,8 @@ public class CommonFormatter {
int endTag = newMessage.indexOf(']', startTag);
String pluginName = colorizePluginName(newMessage.substring(startTag, endTag));
return '[' + pluginName + ']' + startingColorCode + newMessage.substring(endTag + 1);
return '[' + pluginName + ']' + startingColorCode
+ levelColors.getOrDefault(level, "") + newMessage.substring(endTag + 1) + reset;
}
public String colorizePluginName(String pluginName) {
@ -95,8 +111,8 @@ public class CommonFormatter {
return pluginColor + pluginName + reset;
}
public String format(String pluginFormat) {
String[] formatParts = pluginFormat.split(" ");
public String format(String keyCode) {
String[] formatParts = keyCode.split(" ");
Ansi ansi = Ansi.ansi();
for (String format : formatParts) {
for (Code ansiCode : Code.values()) {

View File

@ -68,6 +68,11 @@ public class ColorConsoleConfig {
@Setting(comment = "Removes color formatting if the complete message has color formatting")
private boolean truncateColor;
@Setting(comment = "Should the complete logging message be colored in the same style as the logging level " +
"(like ColorConsole v1).\n" +
"If not it's using the default color (like ColorConsole v2+)")
private boolean colorMessage;
public boolean isColorPluginTag() {
return colorPluginTag;
}
@ -103,4 +108,8 @@ public class ColorConsoleConfig {
public boolean isTruncateColor() {
return truncateColor;
}
public boolean isColorMessage() {
return colorMessage;
}
}

View File

@ -3,6 +3,7 @@ package com.github.games647.colorconsole.sponge;
import com.github.games647.colorconsole.common.ColorAppender;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import org.apache.logging.log4j.core.Appender;
@ -12,7 +13,8 @@ import org.spongepowered.api.Sponge;
public class ColorPluginAppender extends ColorAppender {
public ColorPluginAppender(Appender oldAppender, ColorConsoleConfig config) {
super(oldAppender, config.getHideMessages(), config.isColorPluginTag(), config.isTruncateColor());
super(oldAppender, config.getHideMessages(), config.isColorPluginTag(), config.isTruncateColor()
, config.isColorMessage() ? config.getLevelColors() : Collections.emptyMap());
}
@Override

View File

@ -26,11 +26,11 @@ colorMessage: false
logFormat: '[%d{HH:mm:ss} %level]: %msg%n'
# Log Level Colors
FATAL: red blink
FATAL: red
ERROR: red
WARN: yellow bold
WARN: yellow
INFO: green
DEBUG: green bold
DEBUG: green
TRACE: blue
# Plugin Colors