Reputation: 327
I create a Tomcat webapp project. If I put the log4j.xx.jar & log4j.properties in the Tomcat lib, I get the log file. If I put the log4j.properties in the WEB-INF/classes, I doesn't get the log file. I try again and again. When I put the log4j.xx.jar in the WEB-INF/lib and put the log4j.properties in the WEB-INF/classes, I get it again. Why? What's the logic?
log4j.rootLogger=INFO,A1, A2
#org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=D:\\Tomcat2\\logs\\monitor.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Upvotes: 1
Views: 2031
Reputation: 1840
Put the log4j jar file in WEB-INF/lib
Put log4j.properties anywhere you like eg tomcat/conf . Make sure it is in classpath.
And then read the log4j.properties using the classloader.
YourClass.class.getClassLoader().getResourceAsStream("relative path");
I don't like the idea of putting things to tomcat/lib folders.
Firstly because different apps on tomcat may use different version of log4j.jar. And putting in tomcat/lib will cause a version conflit for some of the applications.
Secondly its not right to put a configuration file in the lib directory. Its right place is the conf folder.
Upvotes: 2
Reputation: 2621
Both log4j.xx.jar
and log4j.properties
files have to be loaded by the same classloader. Tomcat lib files are loaded by a different classloader than WEB-INF/lib files. So you have to put both files under Tomcat lib or both files under WEB-INF/lib to make it work.
Upvotes: 3