File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


JavaRanch » Java Forums » Java » Servlets
Bookmark "using log4j in a servlet" Watch "using log4j in a servlet" New topic
Author

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 log4j.properties 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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

http://forum.java.sun.com/thread.jspa?threadID=488121&messageID=2301917
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.
 
GeeCON Prague 2014
 
subject: using log4j in a servlet