This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes using log4j in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "using log4j in a servlet" Watch "using log4j in a servlet" New topic

using log4j in a servlet

Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi - I'm not sure if this is the correct place to ask, but here goes:

I'm using log4j for logging from my servlet. At present, I have the file located in WEB-INF\classes so that it can be found on the classpath. Unfortunately, it contains paths to the log files, and the app is deployed as a war file, so changing the locations involves rebuilding it...

There's a similar issue with web.xml - it's built into the war file, so changing anything deployment wise means rebuilding the app. This strikes me as mad - am I being dumb and missing something? I thought the whole point was to allow configuration of stuff like this to be done without needing to rebuild?

Thanks in advance for any advice
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
If you look at the log4j docs, you can find something called SetupServlet in a nutshell is a servlet that is checking for changes in the configuration if such happen then it reloads the configuration and applies it run time.

She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Thanks Jim - that helps somewhat - let me see if I've got this right...

I should write a second servlet called for example SetupServlet in whose init method I read an initParameter which specifies where the log4j properties file is located on the filesystem. Having done that, I can set up log4j using a PropertyConfigurator. I need to specify that this servlet is to be loaded by the JVM first by giving it a load-on-startup value smaller than the first servlet.

The point of doing this is so that I'm not dependent on the properties file being on the classpath. This in turn means I can move the log4j properties out of the application war file back into the general filesystem so that changes don't require the war file being rebuilt.

Is that about right?

You mentioned that the servlet could check for changes in the configuration file and apply them at runtime - I've no idea how I'd do that. Any clues? At the moment I think I'd have to restart the container, which would obviously boot everyone out of the application.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Actually you don't have to write it, there's an instance of it posted here:
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Thanks paul - that's exactly what I neeed. I didn't know about the PropertyConfigurator.configureAndWatch method.
I agree. Here's the link:
subject: using log4j in a servlet
It's not a secret anymore!