Add BungeeCord support

This commit is contained in:
games647 2016-05-07 15:07:33 +02:00
parent eeee6d99b6
commit 0cb3d3142f
8 changed files with 280 additions and 10 deletions

16
pom.xml
View File

@ -8,7 +8,7 @@
<packaging>jar</packaging>
<name>ColorConsole</name>
<version>1.4</version>
<version>1.5</version>
<inceptionYear>2016</inceptionYear>
<url>http://dev.bukkit.org/bukkit-plugins/colorconsole/</url>
<description>
@ -83,6 +83,12 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!--BungeeCord with also the part outside the API-->
<repository>
<id>RYRED-REPO</id>
<url>http://mvn.ryred.co/repository/snapshots/</url>
</repository>
</repositories>
<dependencies>
@ -94,6 +100,14 @@
<scope>provided</scope>
</dependency>
<!--Normally should you move this to a extra module, but this project is currently so small-->
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-proxy</artifactId>
<version>1.9-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>

View File

@ -1,4 +1,4 @@
package com.github.games647.colorconsole;
package com.github.games647.colorconsole.bukkit;
import java.io.Serializable;
import java.lang.reflect.Field;
@ -15,7 +15,7 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.bukkit.plugin.java.JavaPlugin;
public class ColorConsole extends JavaPlugin {
public class ColorConsoleBukkit extends JavaPlugin {
private Layout<? extends Serializable> oldLayout;
@ -36,6 +36,7 @@ public class ColorConsole extends JavaPlugin {
@Override
public void onDisable() {
//restore the old format
Appender terminalAppender = getTerminalAppender();
Logger rootLogger = ((Logger) LogManager.getRootLogger());

View File

@ -1,4 +1,4 @@
package com.github.games647.colorconsole;
package com.github.games647.colorconsole.bukkit;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LogEvent;
@ -15,12 +15,12 @@ public class ColorPluginAppender extends AbstractAppender {
private final Appender oldAppender;
private final ColorConsole plugin;
private final ColorConsoleBukkit plugin;
private final String reset = Ansi.ansi().a(Attribute.RESET).toString();
private final String defaultPluginColor;
public ColorPluginAppender(Appender oldAppender, ColorConsole plugin) {
public ColorPluginAppender(Appender oldAppender, ColorConsoleBukkit plugin) {
super(oldAppender.getName(), null, oldAppender.getLayout());
this.plugin = plugin;
@ -51,7 +51,10 @@ public class ColorPluginAppender extends AbstractAppender {
return message;
}
String levelColor = format(plugin.getConfig().getString(level));
String levelColor = "";
if (plugin.getConfig().getBoolean("colorLoggingLevel")) {
levelColor = format(plugin.getConfig().getString(level));
}
int startTag = message.indexOf('[') + 1;
int endTag = message.indexOf(']', startTag);

View File

@ -0,0 +1,84 @@
package com.github.games647.colorconsole.bungee;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.logging.Formatter;
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;
@Override
public void onLoad() {
saveDefaultConfig();
File configFile = new File(getDataFolder(), "config.yml");
try {
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
} catch (IOException ioEx) {
getLogger().log(Level.SEVERE, "Unable to load configuration", ioEx);
return;
}
//try to run it as early as possible
installLogFormat();
}
@Override
public void onDisable() {
//restore the old format
BungeeCord bungee = BungeeCord.getInstance();
Logger bungeeLogger = bungee.getLogger();
Handler[] handlers = bungeeLogger.getHandlers();
for (Handler handler : handlers) {
if (handler instanceof ColouredWriter) {
Formatter formatter = handler.getFormatter();
if (formatter instanceof ColorLogFormatter) {
handler.setFormatter(((ColorLogFormatter) formatter).getOldFormatter());
}
}
}
}
public Configuration getConfiguration() {
return configuration;
}
private void installLogFormat() {
BungeeCord bungee = BungeeCord.getInstance();
Logger bungeeLogger = bungee.getLogger();
Handler[] handlers = bungeeLogger.getHandlers();
for (Handler handler : handlers) {
if (handler instanceof ColouredWriter) {
Formatter oldFormatter = handler.getFormatter();
handler.setFormatter(new ColorLogFormatter(this, oldFormatter));
}
}
}
private void saveDefaultConfig() {
getDataFolder().mkdir();
File configFile = new File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
try (InputStream in = getResourceAsStream("config.yml")) {
Files.copy(in, configFile.toPath());
} catch (IOException ioExc) {
getLogger().log(Level.SEVERE, "Error saving default config", ioExc);
}
}
}
}

View File

@ -0,0 +1,156 @@
package com.github.games647.colorconsole.bungee;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
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 {
private final ColorConsoleBungee plugin;
private final Formatter oldFormatter;
private final DateFormat date = new SimpleDateFormat("HH:mm:ss");
private final String reset = Ansi.ansi().a(Attribute.RESET).toString();
private final String defaultPluginColor;
public ColorLogFormatter(ColorConsoleBungee plugin, Formatter oldFormatter) {
this.plugin = plugin;
this.oldFormatter = oldFormatter;
this.defaultPluginColor = format(plugin.getConfiguration().getString("PLUGIN"));
}
@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());
levelColor = format(plugin.getConfiguration().getString(log4JName));
}
formatted.append(levelColor);
formatted.append(this.date.format(Long.valueOf(record.getMillis())));
formatted.append(" [");
formatted.append(record.getLevel().getName());
formatted.append("] ");
String message = oldFormatter.formatMessage(record);
if (plugin.getConfiguration().getBoolean("colorPluginTag")) {
message = colorizePluginTag(message, levelColor);
}
formatted.append(message);
formatted.append('\n');
if (record.getThrown() != null) {
StringWriter writer = new StringWriter();
record.getThrown().printStackTrace(new PrintWriter(writer));
formatted.append(writer);
}
return formatted.toString();
}
public Formatter getOldFormatter() {
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);
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";
} else if (level == Level.WARNING) {
return "WARN";
} else if (level == Level.INFO) {
return "INFO";
} else if (level == Level.CONFIG) {
return "DEBUG";
} else {
return "TRACE";
}
}
}

View File

@ -0,0 +1,12 @@
# project informations for BungeeCord
# This file will be prioritised over plugin.yml which can be also used for Bungee
# This make it easy to combine BungeeCord and Bukkit support in one plugin
name: ${project.name}
# ${-} will be automatically replaced by Maven
main: ${project.groupId}.${project.artifactId}.bungee.${project.name}Bungee
version: ${project.version}
author: games647, http://github.com/games647/ColorConsole/graphs/contributors
description: |
${project.description}

View File

@ -24,8 +24,8 @@ logFormat: '[%d{HH:mm:ss} %level]: %msg%n'
# Log Level Colors
FATAL: red blink
ERROR: red
WARN: yellow bold BG_Blue
INFO: gray
WARN: yellow bold
INFO: default
DEBUG: green bold
TRACE: blue

View File

@ -2,7 +2,7 @@
# ${-} are variables from Maven (pom.xml) which will be replaced after the build
name: ${project.name}
version: ${project.version}
main: ${project.groupId}.${project.artifactId}.${project.name}
main: ${project.groupId}.${project.artifactId}.bukkit.${project.name}Bukkit
# meta informations for plugin managers
authors: [games647, 'https://github.com/games647/ColorConsole/graphs/contributors']