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:
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
posted
0
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: 1348
posted
0
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.