|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.bristle.javalib.log.Logger
public class Logger
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."); - There is also support for logging in cases where no error can be tolerated. For example: - To log a message without risk of causing an error, even if the local variable "logger" is null: Logger.logSafely(logger, 1, "Line of text to write to the log."); - To log an error message without risk of causing another error, even if the local variable "logger" is null: Logger.logErrorSafely(logger, 1, "An error occurred:", exception); - There is also support for using the Logger in cases where the code that sets up the Logger is not able to pass the Logger object to other code that must use it. The startup code sets the Logger's internal "singleton", which can be used by other parts of the code via the static methods of the Logger. For example: - To set up the Logger's singleton, after configuring it in any of the ways shown above: Logger.setSingleton(logger); - To use the already configured singleton Logger to log a message: Logger.logSafely(1, "Line of text to write to the log."); - To use the already configured singleton Logger to log an error message: Logger.logErrorSafely(1, "An error occurred:", exception); - To modify the configuration of the singleton Logger: Logger.getSingleton().setLogLevel(9); - See the source code of the inner Tester class for more examples. Assumptions: Effects:
Anticipated Changes: Notes: Implementation Notes: Portability Issues: Revision History: $Log$
XMLDOMLoggerTarget
,
ServletContextLoggerTarget
Nested 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. |
Field Summary | |
---|---|
private ArrayList |
m_alTargets
|
private int |
m_intLogLevel
|
private String |
m_strAppName
|
private String |
m_strAppVersion
|
private String |
m_strUsername
|
private static Logger |
st_loggerSingleton
|
Constructor Summary | |
---|---|
Logger()
|
Method Summary | |
---|---|
void |
addTarget(Logger.LoggerTarget target)
Add to the list of LoggerTargets to which logging is done. |
String |
getAppName()
Get the application name. |
String |
getAppVersion()
Get the application version. |
int |
getLogLevel()
Get the log level. |
static Logger |
getSingleton()
Get the singleton Logger, if any. |
String |
getUsername()
Get the username. |
void |
log(int intLogLevel,
String strMsg)
Log a message to the various LoggerTargets. |
static void |
logErrorSafely(int intLogLevel,
String strMsg,
Throwable e)
Log an error message via the singleton Logger, suppressing all possible errors, even the error of having a null value for the singleton Logger. |
static void |
logErrorSafely(Logger logger,
int intLogLevel,
String strMsg,
Throwable e)
Log an error message, suppressing all possible errors, even the error of passing a null value for logger. |
static void |
logSafely(int intLogLevel,
String strMsg)
Log a message via the singleton Logger, suppressing all possible errors, even the error of having a null value for the singleton Logger. |
static void |
logSafely(Logger logger,
int intLogLevel,
String strMsg)
Log a message, suppressing all possible errors, even the error of passing a null value for logger. |
void |
setAppName(String strNew)
Set the application name to be recorded in log entries. |
void |
setAppVersion(String strNew)
Set the application version to be recorded in log entries. |
void |
setLogLevel(int intNew)
Set the log level. |
static void |
setSingleton(Logger logger)
Set the singleton Logger. |
void |
setUsername(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 |
Field Detail |
---|
private static Logger st_loggerSingleton
private int m_intLogLevel
private String m_strAppName
private String m_strAppVersion
private String m_strUsername
private ArrayList m_alTargets
Constructor Detail |
---|
public Logger()
Method Detail |
---|
public void addTarget(Logger.LoggerTarget target)
target
- An additional LoggerTarget to log to.public void setLogLevel(int intNew)
intNew
- The new log level.public int getLogLevel()
public void setAppName(String strNew)
strNew
- The new application name.public String getAppName()
public void setAppVersion(String strNew)
strNew
- The new application version.public String getAppVersion()
public void setUsername(String strNew)
strNew
- The new username.public String getUsername()
public void log(int intLogLevel, String strMsg)
intLogLevel
- Level at which to log the message.strMsg
- String to write to the log entry.public static void logSafely(Logger logger, int intLogLevel, String strMsg)
logger
- The Logger to pass the message to.intLogLevel
- Level at which to log the message.strMsg
- String to write to the log entry.public static void logErrorSafely(Logger logger, int intLogLevel, String strMsg, Throwable e)
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, or null.public static void setSingleton(Logger logger)
logger
- The Logger instance to be stored as the singleton.public static Logger getSingleton()
public static void logSafely(int intLogLevel, String strMsg)
intLogLevel
- Level at which to log the message.strMsg
- String to write to the log entry.public static void logErrorSafely(int intLogLevel, String strMsg, Throwable e)
intLogLevel
- Level at which to log the message.strMsg
- String to write to the log entry.e
- Throwable to include in the message text, or null.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |