uk.org.lidalia.sysoutslf4j.context
Class SysOutOverSLF4J

java.lang.Object
  extended by uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J

public final class SysOutOverSLF4J
extends Object

Public interface to the sysout-over-slf4j module. Provides all methods necessary to manage wrapping the existing System.out and System.err PrintStreams with custom SLF4JPrintStreamImpls that redirect to a logging system via SLF4J. Synchronizes on System.class to ensure proper synchronization even if this class is loaded by multiple classloaders.

Author:
Robert Elliot
See Also:
SLF4JPrintStreamImpl

Method Summary
static boolean isInLoggingSystem(String className)
          Checks whether the given fully qualified class name is a member of a package registered as being a logging system.
static void registerLoggingSystem(String packageName)
          Registers a package as being a logging system and hence any calls to System.out/err println from classes within it should be allowed through to the original PrintStreams rather than redirected to SLF4J.
static void restoreOriginalSystemOutputs()
          If System.out and System.err have been redirected to SLF4J, restores the original PrintStreams allowing direct access to the console again.
This will stop all contexts in the JVM from using sysout-over-slf4j.
static void sendSystemOutAndErrToSLF4J()
          If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context.
Can be called any number of times, and is synchronized on System.class.
Uses the LogPerLineExceptionHandlingStrategyFactory for handling printlns coming from Throwable.printStackTrace().
Logs at info level for System.out and at error level for System.err.
static void sendSystemOutAndErrToSLF4J(ExceptionHandlingStrategyFactory exceptionHandlingStrategyFactory)
          If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class.
static void sendSystemOutAndErrToSLF4J(LogLevel outLevel, LogLevel errLevel)
          If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class.
Uses the LogPerLineExceptionHandlingStrategy for handling printlns coming from Throwable.printStackTrace().
static void sendSystemOutAndErrToSLF4J(LogLevel outLevel, LogLevel errLevel, ExceptionHandlingStrategyFactory exceptionHandlingStrategyFactory)
          If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class.
static void stopSendingSystemOutAndErrToSLF4J()
          Stops using SLF4J for calls to System.out and System.err in the current context.
static boolean systemOutputsAreSLF4JPrintStreams()
           
static void unregisterLoggingSystem(String packageName)
          Unregisters a package as being a logging system and hence any calls to System.out/err println from classes within it will be redirected to SLF4J.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sendSystemOutAndErrToSLF4J

public static void sendSystemOutAndErrToSLF4J()
If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context.
Can be called any number of times, and is synchronized on System.class.
Uses the LogPerLineExceptionHandlingStrategyFactory for handling printlns coming from Throwable.printStackTrace().
Logs at info level for System.out and at error level for System.err.


sendSystemOutAndErrToSLF4J

public static void sendSystemOutAndErrToSLF4J(LogLevel outLevel,
                                              LogLevel errLevel)
If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class.
Uses the LogPerLineExceptionHandlingStrategy for handling printlns coming from Throwable.printStackTrace().

Parameters:
outLevel - The SLF4J LogLevel at which calls to System.out should be logged
errLevel - The SLF4J LogLevel at which calls to System.err should be logged

sendSystemOutAndErrToSLF4J

public static void sendSystemOutAndErrToSLF4J(ExceptionHandlingStrategyFactory exceptionHandlingStrategyFactory)
If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class. Logs at info level for System.out and at error level for System.err.

Parameters:
exceptionHandlingStrategyFactory - The ExceptionHandlingStrategyFactory for creating strategies for handling printlns coming from Throwable.printStackTrace()

sendSystemOutAndErrToSLF4J

public static void sendSystemOutAndErrToSLF4J(LogLevel outLevel,
                                              LogLevel errLevel,
                                              ExceptionHandlingStrategyFactory exceptionHandlingStrategyFactory)
If they have not previously been wrapped, wraps the System.out and System.err PrintStreams in an SLF4JPrintStreamImpl and registers SLF4J for the current context's classloader.
Can be called any number of times, and is synchronized on System.class.

Parameters:
outLevel - The SLF4J LogLevel at which calls to System.out should be logged
errLevel - The SLF4J LogLevel at which calls to System.err should be logged
exceptionHandlingStrategyFactory - The ExceptionHandlingStrategyFactory for creating strategies for handling printlns coming from Throwable.printStackTrace()

stopSendingSystemOutAndErrToSLF4J

public static void stopSendingSystemOutAndErrToSLF4J()
Stops using SLF4J for calls to System.out and System.err in the current context. Has no effect on any other contexts that may be using sysout-over-slf4j.
Can be called any number of times, and is synchronized on System.class.


restoreOriginalSystemOutputs

public static void restoreOriginalSystemOutputs()
If System.out and System.err have been redirected to SLF4J, restores the original PrintStreams allowing direct access to the console again.
This will stop all contexts in the JVM from using sysout-over-slf4j. It is not necessary to call stopSendingSystemOutAndErrToSLF4J() as well as this method. Can be called any number of times, and is synchronized on System.class.


registerLoggingSystem

public static void registerLoggingSystem(String packageName)
Registers a package as being a logging system and hence any calls to System.out/err println from classes within it should be allowed through to the original PrintStreams rather than redirected to SLF4J.

Parameters:
packageName - A package name e.g. org.someloggingsystem

unregisterLoggingSystem

public static void unregisterLoggingSystem(String packageName)
Unregisters a package as being a logging system and hence any calls to System.out/err println from classes within it will be redirected to SLF4J.

Parameters:
packageName - A package name e.g. org.someloggingsystem

isInLoggingSystem

public static boolean isInLoggingSystem(String className)
Checks whether the given fully qualified class name is a member of a package registered as being a logging system.

Parameters:
className - The fully qualifed name of the class which may be in a registered logging system
Returns:
true if the class is in one of the registered logging system packages, false otherwise

systemOutputsAreSLF4JPrintStreams

public static boolean systemOutputsAreSLF4JPrintStreams()


Copyright © 2010. All Rights Reserved.