uk.org.lidalia.sysoutslf4j.system
Class SLF4JPrintStreamImpl

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.PrintStream
              extended by uk.org.lidalia.sysoutslf4j.system.SLF4JPrintStreamImpl
All Implemented Interfaces:
Closeable, Flushable, Appendable, SLF4JPrintStream

public final class SLF4JPrintStreamImpl
extends PrintStream
implements SLF4JPrintStream

Author:
Robert Elliot

Instances of this class wrap the existing System.out or System.err PrintStreams. They intercept all calls to System.out and System.err and log them at info (for System.out) or error (for System.err) level on a logger named after the class which made the call, via an SLF4J Logger.

The sole exceptions are calls to write(byte[]), write(int) and write(byte[], int, int), which pass the calls through to the original PrintStream on the basis that these methods are highly unlikely to be used for "normal" printing to the Console but are used by major logging frameworks in their Console appenders. This allows this interceptor to have a minimal impact for most logging frameworks when sending output to the Console.

Instances should only be created and assigned by the SysOutOverSLF4J helper class.

Calls to Throwable.printStackTrace() and Throwable.printStackTrace(PrintStream) (in the case that the PrintStream passed to the latter is either the System.out or the System.err PrintStream) are handled by calculating the name of the class which called printStackTrace and logging using a logger named after that class.

It is important to note that there are performance overheads for every call to the methods other than write on System.out and System.err using this proxy. It is intended for use with legacy compiled code that has a few calls to System.out/err; it is not intended to encourage use of System.out/err in preference to Loggers in new code. The assumption is that no legacy code prints to System.out or System.err on such a regular basis that the performance hit is heavy, otherwise that code would be rendering the console unusable and itself be a performance drain.

There should be hardly any performance implications for calls to the write methods on System.out/err. LogBack, Log4J and JULI ConsoleAppenders all use the write methods on System.out/err, and so there should be minimal performance overhead for them.

Where an existing logging system needs to use println on System.out/err, this is handled without redirecting that call back to SLF4J, though a performance hit will occur.


Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Method Summary
 PrintStream append(char character)
           
 PrintStream append(CharSequence csq)
           
 PrintStream append(CharSequence csq, int start, int end)
           
 boolean checkError()
           
 void close()
           
 void deregisterLoggerAppender()
           
 void flush()
           
 PrintStream format(Locale locale, String format, Object... args)
           
 PrintStream format(String format, Object... args)
           
 PrintStream getOriginalPrintStream()
           
 void print(boolean bool)
           
 void print(char character)
           
 void print(char[] charArray)
           
 void print(double doubl)
           
 void print(float floa)
           
 void print(int integer)
           
 void print(long lon)
           
 void print(Object object)
           
 void print(String string)
           
 PrintStream printf(Locale locale, String format, Object... args)
           
 PrintStream printf(String format, Object... args)
           
 void println()
           
 void println(boolean bool)
           
 void println(char character)
           
 void println(char[] charArray)
           
 void println(double doub)
           
 void println(float floa)
           
 void println(int integer)
           
 void println(long lon)
           
 void println(Object object)
           
 void println(String string)
           
 void registerLoggerAppender(Object loggerAppenderObject)
           
protected  void setError()
           
 void write(byte[] bytes)
           
 void write(byte[] buf, int off, int len)
           
 void write(int integer)
           
 
Methods inherited from class java.io.PrintStream
clearError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

println

public void println(String string)
Overrides:
println in class PrintStream

println

public void println(Object object)
Overrides:
println in class PrintStream

println

public void println()
Overrides:
println in class PrintStream

println

public void println(boolean bool)
Overrides:
println in class PrintStream

println

public void println(char character)
Overrides:
println in class PrintStream

println

public void println(char[] charArray)
Overrides:
println in class PrintStream

println

public void println(double doub)
Overrides:
println in class PrintStream

println

public void println(float floa)
Overrides:
println in class PrintStream

println

public void println(int integer)
Overrides:
println in class PrintStream

println

public void println(long lon)
Overrides:
println in class PrintStream

append

public PrintStream append(char character)
Specified by:
append in interface Appendable
Overrides:
append in class PrintStream

append

public PrintStream append(CharSequence csq,
                          int start,
                          int end)
Specified by:
append in interface Appendable
Overrides:
append in class PrintStream

append

public PrintStream append(CharSequence csq)
Specified by:
append in interface Appendable
Overrides:
append in class PrintStream

checkError

public boolean checkError()
Overrides:
checkError in class PrintStream

setError

protected void setError()
Overrides:
setError in class PrintStream

close

public void close()
Specified by:
close in interface Closeable
Overrides:
close in class PrintStream

flush

public void flush()
Specified by:
flush in interface Flushable
Overrides:
flush in class PrintStream

format

public PrintStream format(Locale locale,
                          String format,
                          Object... args)
Overrides:
format in class PrintStream

format

public PrintStream format(String format,
                          Object... args)
Overrides:
format in class PrintStream

print

public void print(boolean bool)
Overrides:
print in class PrintStream

print

public void print(char character)
Overrides:
print in class PrintStream

print

public void print(char[] charArray)
Overrides:
print in class PrintStream

print

public void print(double doubl)
Overrides:
print in class PrintStream

print

public void print(float floa)
Overrides:
print in class PrintStream

print

public void print(int integer)
Overrides:
print in class PrintStream

print

public void print(long lon)
Overrides:
print in class PrintStream

print

public void print(Object object)
Overrides:
print in class PrintStream

print

public void print(String string)
Overrides:
print in class PrintStream

printf

public PrintStream printf(Locale locale,
                          String format,
                          Object... args)
Overrides:
printf in class PrintStream

printf

public PrintStream printf(String format,
                          Object... args)
Overrides:
printf in class PrintStream

write

public void write(byte[] buf,
                  int off,
                  int len)
Overrides:
write in class PrintStream

write

public void write(int integer)
Overrides:
write in class PrintStream

write

public void write(byte[] bytes)
           throws IOException
Overrides:
write in class FilterOutputStream
Throws:
IOException

registerLoggerAppender

public void registerLoggerAppender(Object loggerAppenderObject)
Specified by:
registerLoggerAppender in interface SLF4JPrintStream

deregisterLoggerAppender

public void deregisterLoggerAppender()
Specified by:
deregisterLoggerAppender in interface SLF4JPrintStream

getOriginalPrintStream

public PrintStream getOriginalPrintStream()
Specified by:
getOriginalPrintStream in interface SLF4JPrintStream


Copyright © 2011. All Rights Reserved.