From bf673c5d8b965a035ab55753cfbc6038f8a4cfbb Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 29 Jan 2020 12:03:22 +1100 Subject: [PATCH] Add pretty colours to console log levels --- .../net/md_5/bungee/log/BungeeLogger.java | 6 ++-- .../net/md_5/bungee/log/ConciseFormatter.java | 34 ++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java b/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java index 9598fff3..9e1f2569 100644 --- a/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java +++ b/log/src/main/java/net/md_5/bungee/log/BungeeLogger.java @@ -3,7 +3,6 @@ package net.md_5.bungee.log; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; import java.util.logging.FileHandler; -import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -12,7 +11,6 @@ import jline.console.ConsoleReader; public class BungeeLogger extends Logger { - private final Formatter formatter = new ConciseFormatter(); private final LogDispatcher dispatcher = new LogDispatcher( this ); @SuppressWarnings( @@ -28,12 +26,12 @@ public class BungeeLogger extends Logger try { FileHandler fileHandler = new FileHandler( filePattern, 1 << 24, 8, true ); - fileHandler.setFormatter( formatter ); + fileHandler.setFormatter( new ConciseFormatter( false ) ); addHandler( fileHandler ); ColouredWriter consoleHandler = new ColouredWriter( reader ); consoleHandler.setLevel( Level.INFO ); - consoleHandler.setFormatter( formatter ); + consoleHandler.setFormatter( new ConciseFormatter( true ) ); addHandler( consoleHandler ); } catch ( IOException ex ) { diff --git a/log/src/main/java/net/md_5/bungee/log/ConciseFormatter.java b/log/src/main/java/net/md_5/bungee/log/ConciseFormatter.java index 61fdd281..6f3f1585 100644 --- a/log/src/main/java/net/md_5/bungee/log/ConciseFormatter.java +++ b/log/src/main/java/net/md_5/bungee/log/ConciseFormatter.java @@ -5,12 +5,17 @@ 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 lombok.RequiredArgsConstructor; +import net.md_5.bungee.api.ChatColor; +@RequiredArgsConstructor public class ConciseFormatter extends Formatter { private final DateFormat date = new SimpleDateFormat( System.getProperty( "net.md_5.bungee.log-date-format", "HH:mm:ss" ) ); + private final boolean coloured; @Override @SuppressWarnings("ThrowableResultIgnored") @@ -20,7 +25,7 @@ public class ConciseFormatter extends Formatter formatted.append( date.format( record.getMillis() ) ); formatted.append( " [" ); - formatted.append( record.getLevel().getLocalizedName() ); + appendLevel( formatted, record.getLevel() ); formatted.append( "] " ); formatted.append( formatMessage( record ) ); formatted.append( '\n' ); @@ -34,4 +39,31 @@ public class ConciseFormatter extends Formatter return formatted.toString(); } + + private void appendLevel(StringBuilder builder, Level level) + { + if ( !coloured ) + { + builder.append( level.getLocalizedName() ); + return; + } + + ChatColor color; + + if ( level == Level.INFO ) + { + color = ChatColor.BLUE; + } else if ( level == Level.WARNING ) + { + color = ChatColor.YELLOW; + } else if ( level == Level.SEVERE ) + { + color = ChatColor.RED; + } else + { + color = ChatColor.AQUA; + } + + builder.append( color ).append( level.getLocalizedName() ).append( ChatColor.RESET ); + } }