File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using log4j in a servlet

 
Daniel Dalton
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Dalton
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20184
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks paul - that's exactly what I neeed. I didn't know about the PropertyConfigurator.configureAndWatch method.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic