Category Image Quickstart Guide to using Log4j


Log4j has become a standard for logging. It is simple enough to use at the basic level and yet, the granularity of logging can be increased or decreased easily with adjustments to the logger configuration. Enough said, see here quickstart meaty details for using log4j in your WebObjects app....

First learn a little

http://logging.apache.org/log4j/docs/manual.html
http://logging.apache.org/log4j/docs/api/index.html
http://logging.apache.org/log4j/docs/index.html
http://www.vipan.com/htdocs/log4jhelp.html

http://www.onjava.com/pub/a/onjava/2004/09/29/smtp-logging.html

Next download the latest binary and place the log4j-1.2.x.jar into /Library/WebObjects/Extensions

I think 1.2.4 shipped with WO since I had it already, but I still replaced it with 1.2.9 which was the latest one at the time of this article.

Now create a text file for the properties. There are many examples on the web. Here is my current simple one for use as a starting point.

# Log4j configuration file.

log4j.rootCategory=DEBUG, A1, A2

# log4j.rootCategory=DEBUG, A1, A2, A3

# Available levels are DEBUG, INFO, WARN, ERROR, FATAL

#

# A1 is a ConsoleAppender

#

log4j.appender.A1=org.apache.log4j.ConsoleAppender

log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout.ConversionPattern=%-5p [%t] - %m%n

#

# A2 is a DailyRollingFileAppender

#

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender

log4j.appender.A2.file=/Library/Logs/WebObjectsApps/cheetah/log4j_cheetah.log

log4j.appender.A2.datePattern='.'yyyy-MM-dd

log4j.appender.A2.append=true

log4j.appender.A2.layout=org.apache.log4j.PatternLayout

log4j.appender.A2.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n

#

# A3 is a UDPAppender for sending logs as broadcast UDP packets

#

#log4j.appender.A3=org.apache.log4j.net.UDPAppender

#log4j.appender.A3.remoteHost=192.168.15.255

#log4j.appender.A3.port=8881

#log4j.appender.A3.layout=org.apache.log4j.PatternLayout

#log4j.appender.A3.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n


Save it .... for example, I saved mine as
/Library/Preferences/log4j_cheetah.properties

Also make sure the folder you specify for logs in the properties file exists. Log4j will create the log file itself automatically.

OK, now how do we use this in our app.

In your Application class, use something like this (note the import, log declaration and configure statement using the properties file you just made:

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import org.apache.log4j.*;

public class Application extends WOApplication {
private static Logger log;

public static void main(String argv[]) {
WOApplication.main(argv, Application.class);
}

public Application() {
super();
System.out.println("Welcome to " + this.name() + "!");

/* ** Put your application initialization code here ** */

// Configure log4j
PropertyConfigurator.configure("/Library/Preferences/log4j_cheetah.properties");
log = Logger.getLogger( Application.class );
log.debug("Log4j has been initialized");

........etc, etc, etc

Now you can use this in any class you wish by doing something like this (note the import statement, the log declaration statement and the acual logging statements throughout the code if (log.isDebugEnabled() ) log.debug( "log message"); ):
import com.webobjects.eoaccess.*;
import com.webobjects.eocontrol.*;
import com.webobjects.foundation.*;
import java.math.BigDecimal;
import java.util.*;
import org.apache.log4j.Logger;

public abstract class _CTUser extends EOGenericRecord {
private static Logger log = Logger.getLogger( _CTUser.class );

public _CTUser() {
super();
}

public String emailAddress() {
return (String) storedValueForKey("emailAddress");
}

public void setEmailAddress(String aValue) {
if( log.isDebugEnabled() ) log.debug( "updating emailAddress from "+emailAddress()+" to "+aValue );
takeStoredValueForKey(aValue, "emailAddress");
}

public String loginName() {
return (String) storedValueForKey("loginName");
}

public void setLoginName(String aValue) {
if( log.isDebugEnabled() ) log.debug( "updating loginName from "+loginName()+" to "+aValue );
takeStoredValueForKey(aValue, "loginName");
}

public String password() {
return (String) storedValueForKey("password");
}

public void setPassword(String aValue) {
if( log.isDebugEnabled() ) log.debug( "updating password from "+password()+" to "+aValue );
takeStoredValueForKey(aValue, "password");
}

........... etc, etc, etc

And that's basically it. Plus if you want to automate the generation of logging statements in your classes like shown here, you need to use EOGenerator and Jon Rentzsch's templates
http://rentzsch.com/share/eogenerator52templates.zip

... and later just modify your properties file to change the logging behaviour or to turn it off. (You can have one properties file for deployment on your server and another for development on your dev Powerbook !

Also see Chuck's book, Practical WebObjects, pages 22 to 24 for some log4j tips. Also page 399 of Professional WebObjects book.



Posted: Tuesday - November 16, 2004 at 05:16 PM        


Published by