Move net.md_5.bungee.log to its own module.
API subject to change and should not be used externally.
This commit is contained in:
@@ -206,7 +206,7 @@ public class BungeeCord extends ProxyServer
|
||||
consoleReader = new ConsoleReader();
|
||||
consoleReader.setExpandEvents( false );
|
||||
|
||||
logger = new BungeeLogger( this );
|
||||
logger = new BungeeLogger( "BungeeCord", "proxy.log", consoleReader );
|
||||
System.setErr( new PrintStream( new LoggingOutputStream( logger, Level.SEVERE ), true ) );
|
||||
System.setOut( new PrintStream( new LoggingOutputStream( logger, Level.INFO ), true ) );
|
||||
|
||||
|
@@ -1,56 +0,0 @@
|
||||
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;
|
||||
import net.md_5.bungee.BungeeCord;
|
||||
|
||||
public class BungeeLogger extends Logger
|
||||
{
|
||||
|
||||
private final Formatter formatter = new ConciseFormatter();
|
||||
private final LogDispatcher dispatcher = new LogDispatcher( this );
|
||||
|
||||
@SuppressWarnings(
|
||||
{
|
||||
"CallToPrintStackTrace", "CallToThreadStartDuringObjectConstruction"
|
||||
})
|
||||
@SuppressFBWarnings("SC_START_IN_CTOR")
|
||||
public BungeeLogger(BungeeCord bungee)
|
||||
{
|
||||
super( "BungeeCord", null );
|
||||
setLevel( Level.ALL );
|
||||
|
||||
try
|
||||
{
|
||||
FileHandler fileHandler = new FileHandler( "proxy.log", 1 << 24, 8, true );
|
||||
fileHandler.setFormatter( formatter );
|
||||
addHandler( fileHandler );
|
||||
|
||||
ColouredWriter consoleHandler = new ColouredWriter( bungee.getConsoleReader() );
|
||||
consoleHandler.setLevel( Level.INFO );
|
||||
consoleHandler.setFormatter( formatter );
|
||||
addHandler( consoleHandler );
|
||||
} catch ( IOException ex )
|
||||
{
|
||||
System.err.println( "Could not register logger!" );
|
||||
ex.printStackTrace();
|
||||
}
|
||||
dispatcher.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(LogRecord record)
|
||||
{
|
||||
dispatcher.queue( record );
|
||||
}
|
||||
|
||||
void doLog(LogRecord record)
|
||||
{
|
||||
super.log( record );
|
||||
}
|
||||
}
|
@@ -1,83 +0,0 @@
|
||||
package net.md_5.bungee.log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import jline.console.ConsoleReader;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.fusesource.jansi.Ansi;
|
||||
import org.fusesource.jansi.Ansi.Erase;
|
||||
|
||||
public class ColouredWriter extends Handler
|
||||
{
|
||||
|
||||
private final Map<ChatColor, String> replacements = new EnumMap<>( ChatColor.class );
|
||||
private final ChatColor[] colors = ChatColor.values();
|
||||
private final ConsoleReader console;
|
||||
|
||||
public ColouredWriter(ConsoleReader console)
|
||||
{
|
||||
this.console = console;
|
||||
|
||||
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() );
|
||||
}
|
||||
|
||||
public void print(String s)
|
||||
{
|
||||
for ( ChatColor color : colors )
|
||||
{
|
||||
s = s.replaceAll( "(?i)" + color.toString(), replacements.get( color ) );
|
||||
}
|
||||
try
|
||||
{
|
||||
console.print( Ansi.ansi().eraseLine( Erase.ALL ).toString() + ConsoleReader.RESET_LINE + s + Ansi.ansi().reset().toString() );
|
||||
console.drawLine();
|
||||
console.flush();
|
||||
} catch ( IOException ex )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(LogRecord record)
|
||||
{
|
||||
if ( isLoggable( record ) )
|
||||
{
|
||||
print( getFormatter().format( record ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException
|
||||
{
|
||||
}
|
||||
|
||||
}
|
@@ -1,36 +0,0 @@
|
||||
package net.md_5.bungee.log;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class ConciseFormatter extends Formatter
|
||||
{
|
||||
|
||||
private final DateFormat date = new SimpleDateFormat( System.getProperty( "net.md_5.bungee.log-date-format", "HH:mm:ss" ) );
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("ThrowableResultIgnored")
|
||||
public String format(LogRecord record)
|
||||
{
|
||||
StringBuilder formatted = new StringBuilder();
|
||||
|
||||
formatted.append( date.format( record.getMillis() ) );
|
||||
formatted.append( " [" );
|
||||
formatted.append( record.getLevel().getLocalizedName() );
|
||||
formatted.append( "] " );
|
||||
formatted.append( formatMessage( record ) );
|
||||
formatted.append( '\n' );
|
||||
if ( record.getThrown() != null )
|
||||
{
|
||||
StringWriter writer = new StringWriter();
|
||||
record.getThrown().printStackTrace( new PrintWriter( writer ) );
|
||||
formatted.append( writer );
|
||||
}
|
||||
|
||||
return formatted.toString();
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
package net.md_5.bungee.log;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class LogDispatcher extends Thread
|
||||
{
|
||||
|
||||
private final BungeeLogger logger;
|
||||
private final BlockingQueue<LogRecord> queue = new LinkedBlockingQueue<>();
|
||||
|
||||
public LogDispatcher(BungeeLogger logger)
|
||||
{
|
||||
super( "BungeeCord Logger Thread" );
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while ( !isInterrupted() )
|
||||
{
|
||||
LogRecord record;
|
||||
try
|
||||
{
|
||||
record = queue.take();
|
||||
} catch ( InterruptedException ex )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
logger.doLog( record );
|
||||
}
|
||||
for ( LogRecord record : queue )
|
||||
{
|
||||
logger.doLog( record );
|
||||
}
|
||||
}
|
||||
|
||||
public void queue(LogRecord record)
|
||||
{
|
||||
if ( !isInterrupted() )
|
||||
{
|
||||
queue.add( record );
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package net.md_5.bungee.log;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class LoggingOutputStream extends ByteArrayOutputStream
|
||||
{
|
||||
|
||||
private static final String separator = System.getProperty( "line.separator" );
|
||||
/*========================================================================*/
|
||||
private final Logger logger;
|
||||
private final Level level;
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException
|
||||
{
|
||||
String contents = toString( Charsets.UTF_8.name() );
|
||||
super.reset();
|
||||
if ( !contents.isEmpty() && !contents.equals( separator ) )
|
||||
{
|
||||
logger.logp( level, "", "", contents );
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user