Make console log handler obey Filters. Closes #866

This commit is contained in:
md_5 2014-02-04 16:44:53 +11:00
parent df82720ade
commit 224db6596e
2 changed files with 27 additions and 9 deletions

View File

@ -10,22 +10,22 @@ import net.md_5.bungee.BungeeCord;
public class BungeeLogger extends Logger public class BungeeLogger extends Logger
{ {
private final BungeeCord bungee;
private final ColouredWriter writer;
private final Formatter formatter = new ConciseFormatter(); private final Formatter formatter = new ConciseFormatter();
private final LogDispatcher dispatcher = new LogDispatcher( this ); private final LogDispatcher dispatcher = new LogDispatcher( this );
public BungeeLogger(BungeeCord bungee) public BungeeLogger(BungeeCord bungee)
{ {
super( "BungeeCord", null ); super( "BungeeCord", null );
this.bungee = bungee;
this.writer = new ColouredWriter( bungee.getConsoleReader() );
try try
{ {
FileHandler handler = new FileHandler( "proxy.log", 1 << 24, 8, true ); FileHandler fileHandler = new FileHandler( "proxy.log", 1 << 24, 8, true );
handler.setFormatter( formatter ); fileHandler.setFormatter( formatter );
addHandler( handler ); addHandler( fileHandler );
ColouredWriter consoleHandler = new ColouredWriter( bungee.getConsoleReader() );
consoleHandler.setFormatter( formatter );
addHandler( consoleHandler );
} catch ( IOException ex ) } catch ( IOException ex )
{ {
System.err.println( "Could not register logger!" ); System.err.println( "Could not register logger!" );
@ -43,6 +43,5 @@ public class BungeeLogger extends Logger
void doLog(LogRecord record) void doLog(LogRecord record)
{ {
super.log( record ); super.log( record );
writer.print( formatter.format( record ) );
} }
} }

View File

@ -3,11 +3,13 @@ package net.md_5.bungee.log;
import java.io.IOException; import java.io.IOException;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
public class ColouredWriter public class ColouredWriter extends Handler
{ {
private final Map<ChatColor, String> replacements = new EnumMap<>( ChatColor.class ); private final Map<ChatColor, String> replacements = new EnumMap<>( ChatColor.class );
@ -57,4 +59,21 @@ public class ColouredWriter
{ {
} }
} }
@Override
public void publish(LogRecord record)
{
print( getFormatter().format( record ) );
}
@Override
public void flush()
{
}
@Override
public void close() throws SecurityException
{
}
} }