mirror of
https://github.com/games647/ColorConsole.git
synced 2024-11-27 21:06:06 +01:00
Add plugin tag highlighting
This commit is contained in:
parent
4f8b3d404c
commit
b3a825a6c9
9
pom.xml
9
pom.xml
@ -8,7 +8,7 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>ColorConsole</name>
|
<name>ColorConsole</name>
|
||||||
<version>1.0</version>
|
<version>1.1</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>
|
||||||
@ -94,6 +94,13 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.fusesource.jansi</groupId>
|
||||||
|
<artifactId>jansi</artifactId>
|
||||||
|
<version>1.11</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
|
@ -4,11 +4,11 @@ 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.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.core.Appender;
|
import org.apache.logging.log4j.core.Appender;
|
||||||
import org.apache.logging.log4j.core.Layout;
|
import org.apache.logging.log4j.core.Layout;
|
||||||
|
import org.apache.logging.log4j.core.Logger;
|
||||||
import org.apache.logging.log4j.core.LoggerContext;
|
import org.apache.logging.log4j.core.LoggerContext;
|
||||||
import org.apache.logging.log4j.core.config.Configuration;
|
import org.apache.logging.log4j.core.config.Configuration;
|
||||||
import org.apache.logging.log4j.core.config.DefaultConfiguration;
|
import org.apache.logging.log4j.core.config.DefaultConfiguration;
|
||||||
@ -32,40 +32,60 @@ public class ColorConsole extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
Appender terminalAppender = getTerminalAppender();
|
||||||
|
Logger rootLogger = ((Logger) LogManager.getRootLogger());
|
||||||
|
|
||||||
|
ColorPluginAppender colorPluginAppender = null;
|
||||||
|
for (Appender value : rootLogger.getAppenders().values()) {
|
||||||
|
if (value instanceof ColorPluginAppender) {
|
||||||
|
colorPluginAppender = (ColorPluginAppender) value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colorPluginAppender != null) {
|
||||||
|
rootLogger.removeAppender(terminalAppender);
|
||||||
|
rootLogger.addAppender(colorPluginAppender.getOldAppender());
|
||||||
|
}
|
||||||
|
|
||||||
setLayout(oldLayout);
|
setLayout(oldLayout);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void installLogFormat() {
|
private void installLogFormat() {
|
||||||
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
Appender terminalAppender = getTerminalAppender();
|
||||||
Configuration conf = ctx.getConfiguration();
|
|
||||||
|
|
||||||
// ConsoleAppender consoleAppender = (ConsoleAppender) conf.getAppenders().get("WINDOWS_COMPAT");
|
|
||||||
Appender terminalAppender = conf.getAppenders().get("TerminalConsole");
|
|
||||||
|
|
||||||
oldLayout = terminalAppender.getLayout();
|
oldLayout = terminalAppender.getLayout();
|
||||||
|
|
||||||
PatternLayout layout = PatternLayout
|
PatternLayout layout = PatternLayout
|
||||||
.createLayout("[%highlight{%d{HH:mm:ss} %-5level]: %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, "
|
.createLayout("%highlight{[%d{HH:mm:ss} %-5level]: %msg%n}{FATAL=red blink, ERROR=red, "
|
||||||
+ "INFO=gray, DEBUG=green bold, TRACE=blue}", new DefaultConfiguration(), null
|
+ "WARN=yellow bold, INFO=gray, DEBUG=green bold, TRACE=blue}", new DefaultConfiguration()
|
||||||
, Charset.defaultCharset().name(), "true");
|
, null, Charset.defaultCharset().name(), "true");
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
||||||
|
Logger rootLogger = ((Logger) LogManager.getRootLogger());
|
||||||
|
|
||||||
|
ColorPluginAppender pluginAppender = new ColorPluginAppender(terminalAppender);
|
||||||
|
pluginAppender.start();
|
||||||
|
|
||||||
|
rootLogger.removeAppender(terminalAppender);
|
||||||
|
rootLogger.addAppender(pluginAppender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLayout(Layout<? extends Serializable> layout) {
|
private void setLayout(Layout<? extends Serializable> layout) {
|
||||||
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
Appender terminalAppender = getTerminalAppender();
|
||||||
Configuration conf = ctx.getConfiguration();
|
|
||||||
|
|
||||||
Appender terminalAppender = conf.getAppenders().get("TerminalConsole");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Field field = terminalAppender.getClass().getSuperclass().getDeclaredField("layout");
|
Field field = terminalAppender.getClass().getSuperclass().getDeclaredField("layout");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(terminalAppender, layout);
|
field.set(terminalAppender, layout);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(ColorConsole.class.getName()).log(Level.SEVERE, "Failed to install log format", ex);
|
getLogger().log(Level.SEVERE, "Failed to install log format", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(org.apache.logging.log4j.Level.ALL);
|
private Appender getTerminalAppender() {
|
||||||
ctx.updateLoggers(conf);
|
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
||||||
|
Configuration conf = ctx.getConfiguration();
|
||||||
|
|
||||||
|
return conf.getAppenders().get("TerminalConsole");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.github.games647.colorconsole;
|
||||||
|
|
||||||
|
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.fusesource.jansi.Ansi;
|
||||||
|
import org.fusesource.jansi.Ansi.Attribute;
|
||||||
|
|
||||||
|
public class ColorPluginAppender extends AbstractAppender {
|
||||||
|
|
||||||
|
private final Appender oldAppender;
|
||||||
|
|
||||||
|
public ColorPluginAppender(Appender oldAppender) {
|
||||||
|
super(oldAppender.getName(), null, oldAppender.getLayout());
|
||||||
|
|
||||||
|
this.oldAppender = oldAppender;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void append(LogEvent event) {
|
||||||
|
if (isStarted() && oldAppender.isStarted()) {
|
||||||
|
Message newMessage = new SimpleMessage(colorizePluginTag(event.getMessage().getFormattedMessage()));
|
||||||
|
|
||||||
|
LogEvent newEvent = new Log4jLogEvent(event.getLoggerName(), event.getMarker(), event.getFQCN()
|
||||||
|
, event.getLevel(), newMessage, event.getThrown(), event.getContextMap()
|
||||||
|
, event.getContextStack(), event.getThreadName(), event.getSource(), event.getMillis());
|
||||||
|
oldAppender.append(newEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Appender getOldAppender() {
|
||||||
|
return oldAppender;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colorizePluginTag(String message) {
|
||||||
|
if (!message.contains("[") || !message.contains("]")) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user