views:

1223

answers:

6

My servlet application includes a number of library .jars, some of which contain embedded log4j.xml or log4j.properties files. I'd like to ensure that log4j finds my log4j.xml first! I've tried searching for some specification of the priorities of the various classpath elements in a servlet (e.g. does WEB-INF/classes always precede WEB-INF/lib?), or some way to configure or tweak the servlet's classloader so that a given resource directory appears early in the classpath. So far, I've drawn a blank. Any suggestions on ensuring that a servlet .war file loads the correct log4j.xml via the classloader?

A: 

You need to have log4j.properties in your CLASSPATH. The best place is under WEB-INF/classes.

You also have to make sure that you use your version of log4j.jar. So, put it in WEB-INF/lib, just to make sure you are not using one from tomcat folders, since it may cause strange classloading issues.

Dev er dev
That doesn't really answer the question I asked. Is WEB-INF/classes guaranteed to be on the classpath before WEB-INF/lib? What if I want my log4j.xml not to be in WEB-INF/classes but another directory - it's not a class after all.
Ian Dickinson
But it has to be in classpath
Dev er dev
+2  A: 

As far as I understand the resource selection from the classpath is non-deterministic (from the point of view of the app developer). Even if the same file is loaded consistently the behaviour could change: 1. When you upgrade the version of your current container. 2. If you switch containers.

The simplest solution will be to remove embedded log4j config files from library jars. It is almost never a good idea to embed log4j config's as it leads to the problem you are seeing here...

Are they third party jars or jars you developed?

johnstok
Third party, hence the problem.
Ian Dickinson
At the risk of sounding ruthless perhaps you should name and shame them in the question, and file a bug too...
johnstok
I would suggest removing the log4j config file manually from the 3rd party jar. This will be a pain though if you are using maven, ivy, etc. to manage dependencies.
johnstok
Hi johnstok,Normally I do use maven, but I'm working with an intern on this project and it's not mavenized yet. So manually editing the .jars will allow me to see if that's the root cause. Good suggestion, thanks.
Ian Dickinson
Is removing the log4j config from that JAR an option? Also let me guess, is it Axis related?
matt b
+2  A: 

We the Spring Log4jConfigListener in our web.xml file.

You can specify as a context parameter the location of the log4j config file, i.e. you could set it as /WEB-INF/log4j.xml

Would this be an option for you? If you're not using Spring I know that you can set the Log4j location programatically which might also work.

Phill Sacre
Spring isn't an option on this project, but it's a useful tip to bear in mind for the future, so thanks!
Ian Dickinson
A: 

In my experience, WEB-INF/classes typically takes precedence over jars in WEB-INF/lib, however, that also depends on the servlet container you use (I could never figure out the behavior of JRun, for instance). It would help immensely if you could tell me which container you're using.

Also, are you certain that the offending log4j configuration is in a jar in WEB-INF/lib? Typically, when I've run into classpath problems in a servlet container situation, it's because of libraries that reside outside of the web app.

The servlet specs recommend that web app classloaders load their own classes before delegating to the container's classloader (SRV.9.7.2), but since this is counter to the Java spec, not all vendors do this by default (in fact Tomcat is the only container I've used that does this by default). With that said, it's always possible to configure your container's web app classloading behavior. If you tell me which container you're using, I may be able to help you (specifically, I have done this successfully before on WebLogic, WebSphere, Glassfish and JRun)).

Jack Leow
Hi Jack,I'm currently using Tomcat 6 (tomcat6-6.0.18-1.1.fc9.noarch) and Jetty 6.1.11. And no, I'm not wholly sure that the clash is coming from log4j configs in my .jars ... we have some weird logging behaviour, and conflicting configs seems like a good hypothesis atm.
Ian Dickinson
I have not used Jetty, but in Tomcat, you can probably rule out classloader configuration as the problem. Is log4j.jar in your WEB-INF/lib directory?
Jack Leow
A: 

If you're unable to control the classpath, since Tomcat is setting it for you, are you at least able to set a system property for log4j.configuration? I believe that location pointed to by that property can be set outside of the classpath.

If not, another approach, although an ugly one, would be to explicitly run one of the configurators yourself in your application code.

matt b
+3  A: 

the answer is simple, taken from tomcat documentation

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

  • Bootstrap classes of your JVM
  • System class loader classses (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/lib
  • $CATALINA_HOME/lib/*.jar
Yonatan Maman