• 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

using log4j in a servlet

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks paul - that's exactly what I neeed. I didn't know about the PropertyConfigurator.configureAndWatch method.
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic