0

I have a web-app where a log4j.xml has been defined which specifies an appender to output to console only. This web-app is being deployed in Tomcat7 in WIndows environment. I need to include a log4j.xml which would specify appenders to output to the console as well as a file. I have created the log4j.xml and placed it in the conf folder for the same and have followed the steps to bootstrap log4j to tomcat.
PROBLEM:
I have tried modifying the catalina.bat file to update LOGGING_CONFIG to look up CATALINA_HOME/conf/log4j.xml but it did not work.I am unable to configure tomcat to pick the log4j in the conf folder. It always picks the one in the web-app. I was able to get some more info on the problem by including the below lines in catalina.bat file.

rem To get details on the instiation of the log4j
set JAVA_OPTS=-Dlog4j.debug

It gave me the below output

log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
  context:
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@7daf6ecc
.
log4j: Using URL [file:/C:/whp-tomcat-7/webapps/ROOT/WEB-INF/classes/log4j.xml] for automatic log4j configuration.

I figured that it has something to do with the way the the log4j class loader looks up for log4j.xml. Can somebody please help me defining the heirarchy so that I am able to configure Tomcat to pick up the log4j.xml from the conf folder at catalina home?

Thanks in advance

  • Maybe this helps: http://stackoverflow.com/questions/24066664/where-to-put-log4j-xml-on-tomcat-7 – Richard Osseweyer Sep 30 '15 at 11:34
  • Thanks @acdhirr.. It did not work for me. I was however able to narrow it down to this, the classloader StandardClassLoader loads the log4j.xml from the correct path but then log4j tries to locate the log4j.xml using context classloader WebappClassLoader. This is where things go south and it ends up using the log4j present in the war. Is there a way to bypass or maybe restrict the search to the StandardClassLoader. Thanks – Ronnie Horo Sep 30 '15 at 12:39
  • We decided to maintain just one version of log4j.xml file and that resolved the issue. Though I am still not aware if we can control the the manner in which Tomcat class loaders and log4j collaborate to search for the log4j.xml, for now this is what I went ahead with and has resolved the issue of not picking the correct version of log4j.xml – Ronnie Horo Oct 01 '15 at 07:59
  • Did you try setting jvm argument: `-Dlog4j.configuration=file:///path/to/log4j.xml` It should override the file at the default location (in classes) which can just sit there since it will be ignored – Richard Osseweyer Oct 01 '15 at 10:56
  • I tried setting LOGGING_CONFIG property of tomcat with `set LOGGING_CONFIG=-Djava.util.logging.config.file=%CATALINA_HOME%\conf\log4j.xml` But it did not work either. The pattern remained where the `StandardClassLoader` would load the logj at Tomcat and then during `WebappClassLoader`, the log4j in WAR would get loaded. Come to think of it, it might be because I had replaced the `tomcat-juli.jar` as prescribed in the Tomcat 7 documentation. But maintaining two log4j also is a redundant thing for us so we went ahead with just the one log4j.xml located at Tomcat/conf/ folder. – Ronnie Horo Nov 13 '15 at 06:17

0 Answers0