Add pretty colours to console log levels

This commit is contained in:
md_5 2020-01-29 12:03:22 +11:00
parent 2235a32316
commit bf673c5d8b
2 changed files with 35 additions and 5 deletions

View File

@ -3,7 +3,6 @@ package net.md_5.bungee.log;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException; import java.io.IOException;
import java.util.logging.FileHandler; import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -12,7 +11,6 @@ import jline.console.ConsoleReader;
public class BungeeLogger extends Logger public class BungeeLogger extends Logger
{ {
private final Formatter formatter = new ConciseFormatter();
private final LogDispatcher dispatcher = new LogDispatcher( this ); private final LogDispatcher dispatcher = new LogDispatcher( this );
@SuppressWarnings( @SuppressWarnings(
@ -28,12 +26,12 @@ public class BungeeLogger extends Logger
try try
{ {
FileHandler fileHandler = new FileHandler( filePattern, 1 << 24, 8, true ); FileHandler fileHandler = new FileHandler( filePattern, 1 << 24, 8, true );
fileHandler.setFormatter( formatter ); fileHandler.setFormatter( new ConciseFormatter( false ) );
addHandler( fileHandler ); addHandler( fileHandler );
ColouredWriter consoleHandler = new ColouredWriter( reader ); ColouredWriter consoleHandler = new ColouredWriter( reader );
consoleHandler.setLevel( Level.INFO ); consoleHandler.setLevel( Level.INFO );
consoleHandler.setFormatter( formatter ); consoleHandler.setFormatter( new ConciseFormatter( true ) );
addHandler( consoleHandler ); addHandler( consoleHandler );
} catch ( IOException ex ) } catch ( IOException ex )
{ {

View File

@ -5,12 +5,17 @@ import java.io.StringWriter;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ChatColor;
@RequiredArgsConstructor
public class ConciseFormatter extends Formatter 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 DateFormat date = new SimpleDateFormat( System.getProperty( "net.md_5.bungee.log-date-format", "HH:mm:ss" ) );
private final boolean coloured;
@Override @Override
@SuppressWarnings("ThrowableResultIgnored") @SuppressWarnings("ThrowableResultIgnored")
@ -20,7 +25,7 @@ public class ConciseFormatter extends Formatter
formatted.append( date.format( record.getMillis() ) ); formatted.append( date.format( record.getMillis() ) );
formatted.append( " [" ); formatted.append( " [" );
formatted.append( record.getLevel().getLocalizedName() ); appendLevel( formatted, record.getLevel() );
formatted.append( "] " ); formatted.append( "] " );
formatted.append( formatMessage( record ) ); formatted.append( formatMessage( record ) );
formatted.append( '\n' ); formatted.append( '\n' );
@ -34,4 +39,31 @@ public class ConciseFormatter extends Formatter
return formatted.toString(); 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 );
}
} }