aspose file tools*
The moose likes Servlets and the fly likes Log4j with Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Log4j with Servlets" Watch "Log4j with Servlets" New topic
Author

Log4j with Servlets

Sanjay Ra
Ranch Hand

Joined: Nov 05, 2004
Posts: 31
Hi .

I am trying to use log4j with servlets(tomcat)

Does anyone have a good example of this?

thanks!
Sanjay
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The way I do it is to put the code that initializes log4j in one of my servlet's init()method and have that servlet set to initialize at startup. The main command you need to execute is:
PropertyConfigurator.configureAndWatch(propFile);

I then put an instance variable in each servlet like this:

Category logger = Logger.getInstance("myinstance");

the logger object can then perform any of the logging functions in any of the servlet's methods.


Merrill
Consultant, Sima Solutions
Sanjay Ra
Ranch Hand

Joined: Nov 05, 2004
Posts: 31
Hi Merrill,

thanks for the response.

Currently i have declared a static instance variable :

static Log myLog;

and then in my init i call

myLog = LogFactory.getLog(logName);

i then use this logger in my methods

is there a porblem with this?

thanks
Sanjay
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This is fine as long as at some point prior to this you have performed the configureAndWatch method on PropertyConfigurator to set up the log instance and tell it what properties file to use.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
The Category class has been deprecated and has been replaced with Logger see "preparing for log4j 1.3 ". It will compile and work fine with Category right now, but Category will be removed at some point (it sounds like it will happen in 1.3 that is currently in progress).

So, you can replace this:
Category logger = Logger.getInstance("myinstance");

with this:
Logger logger = Logger.getLogger("com.foo");

Also, it isn't absolutely necessary to configure log4 in the code. log4j will look for log4j.xml, then log4j.properties in the classpath. You could use configure() instead of configureAndWatch() (see log4j short manual). The watch part of configureAndWatch means it is supposed to check periodically to see if the properties file changed. Some people have had some threading issues with that. Your mileage may vary.

From the log4j API documentation:

configureAndWatch

public static void configureAndWatch(String configFilename,
long delay)

Read the configuration file configFilename if it exists. Moreover, a thread will be created that will periodically check if configFilename has been created or modified. The period is determined by the delay argument. If a change or file creation is detected, then configFilename is read to configure log4j.


The LogFactory usage (e.g. Log myLog = LogFactory.getLog(logName) ) is from commons logging package which can be configured to log using log4j. Log4j developer Ceki G�lc� has strong feelings about using commons logging, see
Think Again - log4j vs. commons article. I've always used log4j directly.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Log4j with Servlets