About This Code
Brief Description:
log4Dom - consistant logging tool similar to log4J
Category:
Application, Java, Lotusscript
Last Modified:
13 Nov 2003
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
tired of the inconsistant logging using combinations of the Agent Log, Cutom application logs and Print statements I decided to create a one-size-fits-most logging utility based around the apache log4J utility. (htttp://jakarta.apache.org/log4j/).
All the detailed instructions can be found in the Using document and example agents can be found in this database. A java set of classes will follow, so this same system can be used in java agents.
Entries are made in the Log by LotusScript using the log4Dom class. The log levels have been taken from the apache log4j
(http://jakarta.apache.org/log4j/).
Rather than using the standard Agent Log file, Print Statements or Messagebox Statements you use the logging levels in
log4Dom. There are 5 predefined levels
5 - Debug
4 - Info
3 - Warn
2 - Error
1 - Fatal
For each level there is a log feature;
Set log4Dom = New log4dom
log4Dom.debug(" debugging information ")
log4Dom.info(" an informational message ")
log4Dom.warn(" a warning ")
log4Dom.logerror(" an error ")
log4Dom.fatal(" a fatal error ")
Additional levels can be added as a constant or set with the generic feature logger
log4Dom.logger(10, " more detailed information ")
As development or troubleshooting continues you can determine which level of logging that you wish to record by setting the log4Dom log level. The example below will record every log entry, setting the level to 0 or LEVEL_NONE will produce no output at all
Whilst the setting of the log level is programatic, I suspect that depending on each application requirement this could be derived from profile documents, ini files or event a central logging database in which each application and there logging levels could be assigned.
log4Dom.logLevel=10
This level will log all debug and below logs.
log4Dom.logLevel=LEVEL_DEBUG
To distinguish between the different modules, such as agents, function and script libraries you can set which module you are logging about using the module feature.
log4Dom.module = "test agent"
Whilst the code is supplied in a NotesDatabase, the log out out can be directed to (currently) four locations.
To a notes database using this design (LS, Java)
To a text file (LS, Java)
To the domino status bar (LS)
To a sucession of messagebox prompts. (LS)
To the Standard output (Java)
The is controlled by the setLogType feature as below.
Call log4Dom.setLogType(sess, "database", "log4Dom2.nsf")
Call log4Dom.setLogType(sess, "file", "c:\logger.txt")
Call log4Dom.setLogType(sess, "prompt", "")
Call log4Dom.setLogType(sess, "status", "")
log.setLogType(null,"sop",null)
Finally for house keeping you will need to close the log using the close feature. For the database and file format this performs specific command such as saving the log document or closeing the file handle.
log4Dom.close
To use the log4dom class copy the script library to the application that you wish to start logging, see the example agent supplied for working examples. NOTE : the database logs to a database called log4Dom2.nsf, you will need to create one from this design.
The output of the log is in the format;
<date and time> <log level> <module> - <message>
Example;
5/11/2003 12:49:28 PM [INFO] test agent - testing the info
5/11/2003 12:49:28 PM [WARN] test agent - testing the warn
5/11/2003 12:49:28 PM [ERROR] test agent - testing the error
5/11/2003 12:49:28 PM [FATAL] test agent - testing the fatal
5/11/2003 12:49:28 PM [DEBUG] test agent - testing the debug
5/11/2003 12:49:28 PM [LEVEL 10] test agent - testing 10
Usage / Example
LS version, see above
Java Version :
log4Dom log = new log4Dom();
log.setLogLevel(4);
log.setModule("JavaAgent Test");
//log.setLogType(null, "file","c:/log_java.txt");
//log.setLogType(session, "database","log4Dom2.nsf");
log.setLogType(null,"sop","");
log.info("Info message");
log.debug("debug message");
log.error("error message");
log.warn("warn message");
log.log(10,"Level message");
log.fatal("fatal error");
log.close();
NOTE : now that I've installed eclipse I have attached a log4Dom.jar. You will need to import com.tp.log4Dom
Code Attachments
Comments
Posted by Starrow Pan on 04/09/2007 05:51:40 AMIt's cool
My company's product use a self-defined class and database for logging.I've thought about making one like the log4j.Seems you have the same idea.
Posted by tony palmer on 04/09/2007 06:05:30 PMThanks
It's been a while since I first created it but I use it on most projects. Its lightweight in comparison to OpenLog.