The Logger is a quick way of logging messages of different
priorities.
Usage
In it's simplest form, place the following field in the class
that uses the debugging:
private static Logger log = new Logger(ThisClassName.class);
Then to log messages, in your code use:
log.info("Debug message");
You can have several Loggers in the same class by using the ( Class, String )
constructor as follows:
Logger logger = newLogger( ThisClassName.class ); // For general logging.
Logger loopLogger = newLogger( ThisClassName.class, "loop" ); // Just for logging my loop.
This will create a Logger with context com.somecompany.thispackage.ThisClassName.loop
whose priority can be set from a config file independently of the main logger
com.somecompany.thispackage.ThisClassName
.
That's about it really. The types of log messages available
(in order of severity - least first) are:
-
#debug(java.lang.Object)
-
-
#info(java.lang.Object)
-
-
#warn(java.lang.Object)
-
-
#error(java.lang.Object)
-
-
#fatal(java.lang.Object)
-
Each of these methods has a variation accepting a second parameter
of type Throwable. This is useful for logging Exceptions or Errors that
are thrown.
There are corresponding methods for
#isDebugEnabled() ,
#isInfoEnabled() etc. to save the overhead of building the log string
if the appropriate level is not set, as follows:
if ( logger.isInfoEnabled() ) logger.info( "This " + methodCall() + " and the string "
+ "concatenation will only take place if "
+ "INFO debugging is currently enabled" );
The actual logging mechanism used depends on the configured
com.opensymphony.provider.LogProvider - this can be set
using the logger.provider system property. If not specified,
com.opensymphony.provider.log.DefaultLogProvider is used.