• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Log4j with Servlets

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi .

I am trying to use log4j with servlets(tomcat)

Does anyone have a good example of this?

thanks!
Sanjay
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sanjay Ra
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic