Monday, April 16, 2007

stoken-utils logger

Overview
On occasion a simple logger comes in handy. Granted log4j rules and will solve all your enterprise level logging needs, but in some cases, you just want something written to a file with minimal fuss. That is where the the logger comes in. It is part of a small library of utilities called stoken-utils.

Project Information
Download: http://code.google.com/p/stoken-utils/

Usage
Using logger is easy. There are several overloaded static methods. The one with the most options is as follows:
public static boolean writeLog(
String PROGNAME,
String MODULE,
TYPE type,
String msg,
boolean addblank
);
PROGNAME - name of your program (or AppInfo.getPROGNAME())
MODULE - usually CLASSNAME::METHOD for me but this is free form.
TYPE - Logger.TYPE enum of Debug, Warn, Error, ....
msg - log message in freeform text.
addblank - adds a blank line after this log message. Usually used before your program shutdown.

The overloaded versions are all described in the javadoc which is downloadable as a zip.

Example
The following
Logger.writeLog("PROGNAME","CLASS::METHOD",TYPE.Debug,"Message",false);
You will end up with a file called PROGNAME.log with the following values:

2007-04-14 00:37:13 [Debug](CLASS::METHOD) Message
File Location
The location where the log files are written can be controlled using several static members.
LOG_FILE_DIR - path to log location like /tmp/logs
LOG_BACKUP_DIR - subdirectory where backups are automatically written (default is "backuplogs/")
LOG_FILE_EXT - extension to add to log file name. (default is ".log")

Behavior

When using Logger, you can expect it to create a backup of your log each day. The backups are placed in the directory pointed to by LOG_BACKUP_DIR. The current log will be appended to always so no data is lost.

I personally like to create a constant in each class called CLASSNAME and a second constant in each method called METHOD. For my MODULE parameter, I can then pass CLASSNAME + "::" + METHOD each time I want to write a log line.

Final Thoughts
I have found this logger to be useful and have used it in many projects (big and small).

No comments: