2013-12-29 06:47:19 +00:00
|
|
|
package com.ryanmichela.sshd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright 2013 Ryan Michela
|
|
|
|
*/
|
|
|
|
|
2017-11-14 15:44:27 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.fusesource.jansi.Ansi;
|
|
|
|
|
2013-12-29 06:47:19 +00:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
import java.text.SimpleDateFormat;
|
2017-11-14 15:44:27 +00:00
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.Map;
|
2013-12-29 06:47:19 +00:00
|
|
|
import java.util.logging.Formatter;
|
|
|
|
import java.util.logging.LogRecord;
|
|
|
|
|
|
|
|
public class ConsoleLogFormatter extends Formatter {
|
|
|
|
|
|
|
|
private SimpleDateFormat dateFormat;
|
|
|
|
|
|
|
|
public ConsoleLogFormatter() {
|
|
|
|
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
}
|
|
|
|
|
|
|
|
public String format(LogRecord logrecord) {
|
2017-11-14 15:44:27 +00:00
|
|
|
try {
|
|
|
|
Class.forName("org.bukkit.craftbukkit.command.ColouredConsoleSender");
|
|
|
|
} catch (ClassNotFoundException ignored) {
|
|
|
|
// MEANS WE'RE ON PAPER/TACO/OTHER SHIT
|
|
|
|
colorize(logrecord);
|
|
|
|
}
|
2013-12-29 06:47:19 +00:00
|
|
|
StringBuilder stringbuilder = new StringBuilder();
|
|
|
|
|
|
|
|
stringbuilder.append(" [");
|
2017-11-14 15:44:27 +00:00
|
|
|
stringbuilder.append(this.dateFormat.format(logrecord.getMillis())).append(" ");
|
2013-12-29 06:47:19 +00:00
|
|
|
|
|
|
|
stringbuilder.append(logrecord.getLevel().getName()).append("]: ");
|
|
|
|
stringbuilder.append(this.formatMessage(logrecord));
|
|
|
|
stringbuilder.append('\n');
|
|
|
|
Throwable throwable = logrecord.getThrown();
|
|
|
|
|
|
|
|
if (throwable != null) {
|
|
|
|
StringWriter stringwriter = new StringWriter();
|
|
|
|
|
|
|
|
throwable.printStackTrace(new PrintWriter(stringwriter));
|
|
|
|
stringbuilder.append(stringwriter.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringbuilder.toString();
|
|
|
|
}
|
2017-11-14 15:44:27 +00:00
|
|
|
|
|
|
|
private void colorize(LogRecord logrecord) {
|
|
|
|
// ORIGINAL CODE FROM org.bukkit.craftbukkit.command.ColouredConsoleSender
|
|
|
|
final Map<ChatColor, String> replacements = new EnumMap<>(ChatColor.class);
|
|
|
|
|
|
|
|
replacements.put(ChatColor.BLACK, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.GOLD, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString());
|
|
|
|
replacements.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString());
|
|
|
|
replacements.put(ChatColor.BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString());
|
|
|
|
replacements.put(ChatColor.GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString());
|
|
|
|
replacements.put(ChatColor.AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString());
|
|
|
|
replacements.put(ChatColor.RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).bold().toString());
|
|
|
|
replacements.put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString());
|
|
|
|
replacements.put(ChatColor.YELLOW, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString());
|
|
|
|
replacements.put(ChatColor.WHITE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString());
|
|
|
|
replacements.put(ChatColor.MAGIC, Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString());
|
|
|
|
replacements.put(ChatColor.BOLD, Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString());
|
|
|
|
replacements.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString());
|
|
|
|
replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString());
|
|
|
|
replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Ansi.Attribute.ITALIC).toString());
|
|
|
|
replacements.put(ChatColor.RESET, Ansi.ansi().a(Ansi.Attribute.RESET).toString());
|
|
|
|
|
|
|
|
String result = logrecord.getMessage();
|
|
|
|
for (ChatColor color : ChatColor.values()) {
|
|
|
|
if (replacements.containsKey(color)) {
|
|
|
|
result = result.replaceAll("(?i)" + color.toString(), replacements.get(color));
|
|
|
|
} else {
|
|
|
|
result = result.replaceAll("(?i)" + color.toString(), "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += Ansi.ansi().reset().toString();
|
|
|
|
logrecord.setMessage(result);
|
|
|
|
}
|
2013-12-29 06:47:19 +00:00
|
|
|
}
|
|
|
|
|