Class Logger

java.lang.Object
org.testng.log4testng.Logger

public class Logger extends Object
TestNG support logging via a custom logging framework similar to Log4j. To control logging, add a resource named "log4testng.properties" to your classpath. The logging levels are TRACE, DEBUG, INFO, WARN, ERROR and FATAL. The Logging framework has the following characteristics:
  • All logging is done using System.out (for levels < ERROR) or System.err. There is no way to specify Appenders.
  • There is no way to control logging programmatically.
  • The log4testng.properties resource is searched in the classpath on the first call to the logging API. If it is not present, logging defaults to the WARN level.
The property file contains lines in the following format:

 # log4testng will log its own behavior (generally used for debugging this package only).
 log4testng.debug=true

 # Specifies the root Loggers logging level. Will log DEBUG level and above
 log4testng.rootLogger=DEBUG

 # The org.testng.reporters.EmailableReporter Logger will log TRACE level and above
 log4testng.logger.org.testng.reporters.EmailableReporter=TRACE

 # All Logger in packages below org.testng will log WARN level and above
 log4testng.logger.org.testng=WARN
 
In your source files you will typically instantiate and use loggers this ways:

 import org.testng.log4testng.Logger;

 class ThisClass {
     private static final Logger LOGGER = Logger.getLogger(ThisClass.class);

     ...
     LOGGER.debug("entering myMethod()");
     ...
     LOGGER.warn("unknown file: " + filename);
     ...
     LOGGER.error("Unexpected error", exception);
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static boolean
    Should log4testng log what it is doing (defaults to false).
    private static final int
     
    private static final String
    Debug property name in log4testng.properties.
    private static PrintStream
    The standard error stream (this is allways System.err except for unit tests)
    private static final int
     
    private static final int
     
    private static int
     
    private static final int
     
    private static boolean
    true if the Logging system has been initialized.
    private final Class
    The logger's name.
    private final int
    The logger's level
    private static final int
     
    private static final Map<String,Integer>
    A map from level name to level integer index (TRACE->0, DEBUG->1 ...)
    private static final String[]
    An ordered list of level names.
    private static final String
    Standard prefix of all logger names in log4testng.properties.
    private static final Map<String,Integer>
    Map from Logger names to level index (as specified in log4testng.properties)
    private static final Map<Class,Logger>
    Map of all known loggers.
    private final String
     
    private static PrintStream
    The standard output stream (this is allways System.out except for unit tests)
    private static final String
    Standard prefix of all property names in log4testng.properties.
    private static final String
    Root logger name in log4testng.properties.
    private static int
    The logging level of the root logger (defaults to warn).
    private static final int
     
    private static final int
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Logger(Class pClass, int pLevel)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
     
    void
    debug(Object message)
    Log a message object with the DEBUG level.
    void
    debug(Object message, Throwable t)
    Log a message object with the DEBUG level including the stack trace of the Throwable t passed as parameter.
    void
    error(Object message)
    Log a message object with the ERROR level.
    void
    error(Object message, Throwable t)
    Log a message object with the DEBUG level including the stack trace of the Throwable t passed as parameter.
    void
    fatal(Object message)
    Log a message object with the FATAL level.
    void
    fatal(Object message, Throwable t)
    Log a message object with the FATAL level including the stack trace of the Throwable t passed as parameter.
    private static int
    getLevel(Class pClass)
    Returns the level associated to the current class.
    static Logger
    getLogger(Class pClass)
    Retrieve a logger named according to the value of the pClass.getName() parameter.
    void
    info(Object message)
    Log a message object with the INFO level.
    void
    info(Object message, Throwable t)
    Log a message object with the WARN level including the stack trace of the Throwable t passed as parameter.
    private static void
     
    boolean
    Check whether this logger is enabled for the DEBUG Level.
    boolean
    Check whether this logger is enabled for the INFO Level.
    private boolean
    isLevelEnabled(int pLevel)
     
    boolean
    Check whether this logger is enabled for the TRACE Level.
    private void
    log(int pLevel, Object pMessage, Throwable pT)
     
    private static void
    Logs the message to System.out of debug is on.
    static void
    main(String[] pArgs)
    Run all tests.
    private static void
    Makes sure the default debug value is false.
    private static void
    Makes sure an illegal debug value throws an exception.
    private static void
    Makes sure the debug value can be turned off and logs nothing.
    private static void
    Makes sure the debug value can be turned on and actualls logs something.
    private static void
    testInitialize(Properties pProperties, PrintStream pOut, PrintStream pErr)
    This method is for debugging purpose only.
    private static void
    Tests that the root logger's default level is WARN and that loggers do not log bellow this level and do log in the correct stream for levels equal to and above WARN.
    private static void
    Test setting the root logger level
    private static void
    Test setting the root logger to an illegal level value throws an exception.
    private static void
    Test setting a user logger level
    private static void
    Test setting a user logger to an illegal level value throws an exception
    private static void
    Tests setting a partial logger name (a hierarchy scope)
    void
    trace(Object message)
    Log a message object with the TRACE level.
    void
    trace(Object message, Throwable t)
    Log a message object with the TRACE level including the stack trace of the Throwable t passed as parameter.
    void
    warn(Object message)
    Log a message object with the WARN level.
    void
    warn(Object message, Throwable t)
    Log a message object with the ERROR level including the stack trace of the Throwable t passed as parameter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • i

      private static int i
    • TRACE

      private static final int TRACE
    • DEBUG

      private static final int DEBUG
    • INFO

      private static final int INFO
    • WARN

      private static final int WARN
    • ERROR

      private static final int ERROR
    • FATAL

      private static final int FATAL
    • LEVEL_COUNT

      private static final int LEVEL_COUNT
    • PREFIX

      private static final String PREFIX
      Standard prefix of all property names in log4testng.properties.
      See Also:
    • LOGGER_PREFIX

      private static final String LOGGER_PREFIX
      Standard prefix of all logger names in log4testng.properties.
      See Also:
    • ROOT_LOGGER

      private static final String ROOT_LOGGER
      Root logger name in log4testng.properties.
      See Also:
    • DEBUG_PROPERTY

      private static final String DEBUG_PROPERTY
      Debug property name in log4testng.properties.
      See Also:
    • err

      private static PrintStream err
      The standard error stream (this is allways System.err except for unit tests)
    • out

      private static PrintStream out
      The standard output stream (this is allways System.out except for unit tests)
    • levelNames

      private static final String[] levelNames
      An ordered list of level names.
    • levelMap

      private static final Map<String,Integer> levelMap
      A map from level name to level integer index (TRACE->0, DEBUG->1 ...)
    • initialized

      private static boolean initialized
      true if the Logging system has been initialized.
    • loggerLevels

      private static final Map<String,Integer> loggerLevels
      Map from Logger names to level index (as specified in log4testng.properties)
    • loggers

      private static final Map<Class,Logger> loggers
      Map of all known loggers.
    • rootLoggerLevel

      private static int rootLoggerLevel
      The logging level of the root logger (defaults to warn).
    • debug

      private static boolean debug
      Should log4testng log what it is doing (defaults to false).
    • level

      private final int level
      The logger's level
    • klass

      private final Class klass
      The logger's name.
    • m_className

      private final String m_className
  • Constructor Details

    • Logger

      private Logger(Class pClass, int pLevel)
  • Method Details

    • getLogger

      public static Logger getLogger(Class pClass)
      Retrieve a logger named according to the value of the pClass.getName() parameter. If the named logger already exists, then the existing instance will be returned. Otherwise, a new instance is created. By default, loggers do not have a set level but inherit it from their nearest ancestor with a set level.
      Parameters:
      pClass - The class' logger to retrieve.
      Returns:
      a logger named according to the value of the pClass.getName().
    • isTraceEnabled

      public boolean isTraceEnabled()
      Check whether this logger is enabled for the TRACE Level.
      Returns:
      true if this logger is enabled for level TRACE, false otherwise.
    • trace

      public void trace(Object message)
      Log a message object with the TRACE level. This method first checks if this logger is TRACE enabled. If this logger is TRACE enabled, then it converts the message object (passed as parameter) to a string by invoking toString(). WARNING Note that passing a Throwable to this method will print the name of the Throwable but no stack trace. To print a stack trace use the trace(Object, Throwable) form instead.
      Parameters:
      message - the message object to log.
    • trace

      public void trace(Object message, Throwable t)
      Log a message object with the TRACE level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • isDebugEnabled

      public boolean isDebugEnabled()
      Check whether this logger is enabled for the DEBUG Level.
      Returns:
      true if this logger is enabled for level DEBUG, false otherwise.
    • debug

      public void debug(Object message)
      Log a message object with the DEBUG level. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
    • debug

      public void debug(Object message, Throwable t)
      Log a message object with the DEBUG level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object, Throwable) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • isInfoEnabled

      public boolean isInfoEnabled()
      Check whether this logger is enabled for the INFO Level.
      Returns:
      true if this logger is enabled for level INFO, false otherwise.
    • info

      public void info(Object message)
      Log a message object with the INFO level. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
    • info

      public void info(Object message, Throwable t)
      Log a message object with the WARN level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object, Throwable) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • warn

      public void warn(Object message)
      Log a message object with the WARN level. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
    • warn

      public void warn(Object message, Throwable t)
      Log a message object with the ERROR level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object, Throwable) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • error

      public void error(Object message)
      Log a message object with the ERROR level. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
    • error

      public void error(Object message, Throwable t)
      Log a message object with the DEBUG level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object, Throwable) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • fatal

      public void fatal(Object message)
      Log a message object with the FATAL level. See Logger.trace(Object) form for more detailed information.
      Parameters:
      message - the message object to log.
    • fatal

      public void fatal(Object message, Throwable t)
      Log a message object with the FATAL level including the stack trace of the Throwable t passed as parameter. See Logger.trace(Object, Throwable) form for more detailed information.
      Parameters:
      message - the message object to log.
      t - the exception to log, including its stack trace.
    • initialize

      private static void initialize()
    • checkProperties

      private static void checkProperties(Properties pProperties)
    • getLevel

      private static int getLevel(Class pClass)
      Returns the level associated to the current class. The level is obtain by searching for a logger in the "testng-logging.properties" resource. For example, if class is "org.testng.TestNG" the the following loggers are searched in this order:
      1. "org.testng.TestNG"
      2. "org.testng"
      3. "org"
      4. The root level
      Parameters:
      pClass - the class name used for logger name.
      Returns:
      the level associated to the current class.
    • isLevelEnabled

      private boolean isLevelEnabled(int pLevel)
    • log

      private void log(int pLevel, Object pMessage, Throwable pT)
    • loglog4testng

      private static void loglog4testng(String pmessage)
      Logs the message to System.out of debug is on.
      Parameters:
      pmessage - the message to log to the console
    • testInitialize

      private static void testInitialize(Properties pProperties, PrintStream pOut, PrintStream pErr)
      This method is for debugging purpose only.
      Parameters:
      pProperties - a properties bundle initialised as log4testng property file would be.
      pOut - the standard output stream to be used for logging.
      pErr - the standard error stream to be used for logging.
    • testDebugDefault

      private static void testDebugDefault()
      Makes sure the default debug value is false.
    • testDebugOn

      private static void testDebugOn()
      Makes sure the debug value can be turned on and actualls logs something.
    • testDebugOff

      private static void testDebugOff()
      Makes sure the debug value can be turned off and logs nothing.
    • testDebugError

      private static void testDebugError()
      Makes sure an illegal debug value throws an exception.
    • testRootLoggerDefault

      private static void testRootLoggerDefault()
      Tests that the root logger's default level is WARN and that loggers do not log bellow this level and do log in the correct stream for levels equal to and above WARN.
    • testRootLoggerSet

      private static void testRootLoggerSet()
      Test setting the root logger level
    • testRootLoggerSetError

      private static void testRootLoggerSetError()
      Test setting the root logger to an illegal level value throws an exception.
    • testUserLoggerSet

      private static void testUserLoggerSet()
      Test setting a user logger level
    • testUserLoggerSetError

      private static void testUserLoggerSetError()
      Test setting a user logger to an illegal level value throws an exception
    • testUserLoggerSetHierarchy

      private static void testUserLoggerSetHierarchy()
      Tests setting a partial logger name (a hierarchy scope)
    • main

      public static void main(String[] pArgs)
      Run all tests. (very crusty ...)
      Parameters:
      pArgs - not used