Class Logger

java.lang.Object
  |
  +--Logger

public class Logger
extends java.lang.Object

This class handles logging of messages to one or more Logger.LoggerTargets (XML DOM, file, Writer, ServletContext, etc.)

Usage:
   - The typical scenario for using this class is:
     - To log to an XML DOM:
           Logger logger = new Logger();
           logger.addTarget(new XMLDOMLoggerTarget(xmlDOM));
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the log.");
     - To log to a file:
           Logger logger = new Logger();
           logger.addTarget(new Logger.FilenameLoggerTarget("/my/log/file"));
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the log.");
     - To log to a Writer:
           Logger logger = new Logger();
           logger.addTarget(new Logger.WriterLoggerTarget(writer));
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the log.");
     - To log to a ServletContext:
           Logger logger = new Logger();
           logger.addTarget(new ServletContextLoggerTarget(objServletContext));
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the log.");
     - To log to multiple targets:
           Logger logger = new Logger();
           logger.addTarget(new XMLDOMLoggerTarget(xmlDOM));
           logger.addTarget(new XMLDOMLoggerTarget(xmlDOM2));
           ...
           logger.addTarget(new Logger.FilenameLoggerTarget("/my/log/file"));
           logger.addTarget(new Logger.FilenameLoggerTarget("/my/log/file2"));
           ...
           logger.addTarget(new Logger.WriterLoggerTarget(writer));
           logger.addTarget(new Logger.WriterLoggerTarget(writer2));
           ...
           logger.addTarget(new ServletContextLoggerTarget(objServletContext));
           logger.addTarget(new ServletContextLoggerTarget(objServletContext2));
           ...
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the logs.");
     - To log to System.out:
           Logger logger = new Logger();
           logger.addTarget(new Logger.WriterLoggerTarget
                                       (new PrintWriter(System.out)));
           logger.setLogLevel(2);
           logger.log(1, "Line of text to write to the log.");
Assumptions:
Effects:
Writes log entries to one or more LoggerTargets, typically in the format: FStluka [Thread-12] 992355259441 23510888 27770872 1 This is a sample log message. Where the space-separated parts are: <Username> is the name of the current user. <ThreadName> is the name of the current thread. <Time> is the current time in milliseconds on the server. <UsedMem> is the current number of bytes of used memory in the JVM of the Web Server. <TotalMem> is the current number of bytes of total memory (free and used) in the JVM of the Web Server. <Level> indicates the logging level of the message. Can be used as an indentation level to format the messages. <Msg> is the text of the message.
Anticipated Changes:
   - Format the data in the log file better (date/time, nesting levels, etc.)?
     Or leave that job to a log file viewer?
Notes:
Implementation Notes:
Portability Issues:

Since:
1.0.0
Version:
$Revision: 11 $
Author:
$Author: FStluka $

Inner Class Summary
static class Logger.Entry
          This class represents a log entry that can be sent by a Logger to a LoggerTarget.
static class Logger.FilenameLoggerTarget
          This class implements the LoggerTarget interface, writing log entries to a named text file.
static interface Logger.LoggerTarget
          This interface must be implemented by any class that expects to be called by the Logger class to log entries to a target (XML DOM, file, Writer, ServletContext, etc.)
static class Logger.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
static class Logger.WriterLoggerTarget
          This class implements the LoggerTarget interface, writing log entries to a Writer.
 
Constructor Summary
Logger()
          Constructor.
 
Method Summary
 void addTarget(Logger.LoggerTarget target)
          Add to the list of LoggerTargets to which logging is done.
 int getLogLevel()
          Get the log level.
 java.lang.String getUsername()
          Get the username.
 void log(int intLogLevel, java.lang.String strMsg)
          Log a message to the various LoggerTargets.
static void logErrorSafely(Logger logger, int intLogLevel, java.lang.String strMsg, java.lang.Throwable e)
          Log an error message, suppressing all possible errors, even the error of passing a null value for logger.
static void logSafely(Logger logger, int intLogLevel, java.lang.String strMsg)
          Log a message, suppressing all possible errors, even the error of passing a null value for logger.
 void setLogLevel(int intNew)
          Set the log level.
 void setUsername(java.lang.String strNew)
          Set the username to be recorded in log entries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Logger

public Logger()
Constructor.
Throws:
Exceptions - None.
Method Detail

addTarget

public void addTarget(Logger.LoggerTarget target)
Add to the list of LoggerTargets to which logging is done.
Parameters:
target - An additional LoggerTarget to log to.
Returns:
None.
Throws:
Exceptions - None.

setLogLevel

public void setLogLevel(int intNew)
Set the log level. Each call to log() with a logLevel greater than this value will be ignored. Only calls with logLevel less than or equal to this value are logged. Default = 1.
Parameters:
intNew - The new log level.
Returns:
None.
Throws:
Exceptions - None.

getLogLevel

public int getLogLevel()
Get the log level.
Returns:
The log level.
Throws:
Exceptions - None.

setUsername

public void setUsername(java.lang.String strNew)
Set the username to be recorded in log entries. Default = current logged in username.
Parameters:
strNew - The new username.
Returns:
None.
Throws:
Exceptions - None.

getUsername

public java.lang.String getUsername()
Get the username.
Returns:
The username.
Throws:
Exceptions - None.

log

public void log(int intLogLevel,
                java.lang.String strMsg)
Log a message to the various LoggerTargets. Compares the specified log level with the current log level. Logs the message if the specified level is less than or equal to the current level.
Parameters:
intLoglevel - Level at which to log the message.
strMsg - String to write to the log entry.
Returns:
None.
Throws:
Exceptions - None.

logSafely

public static void logSafely(Logger logger,
                             int intLogLevel,
                             java.lang.String strMsg)
Log a message, suppressing all possible errors, even the error of passing a null value for logger. It is safe to call this method from an exception handler or finally clause without fear of throwing another exception.
Parameters:
logger - The Logger to pass the message to.
intLoglevel - Level at which to log the message.
strMsg - String to write to the log entry.
Returns:
None.
Throws:
Exceptions - None.

logErrorSafely

public static void logErrorSafely(Logger logger,
                                  int intLogLevel,
                                  java.lang.String strMsg,
                                  java.lang.Throwable e)
Log an error message, suppressing all possible errors, even the error of passing a null value for logger. It is safe to call this method from an exception handler or finally clause without fear of throwing another exception.
Parameters:
logger - The Logger to pass the message to.
intLoglevel - Level at which to log the message.
strMsg - String to write to the log entry.
e - Throwable to include in the message text.
Returns:
None.
Throws:
Exceptions - None.