If you use Spring, you can:
1) create a log4j configuration file, for example. "/WEB-INF/classes/log4j-myapp.properties" Do not call it "log4j.properties"
Example:
log4j.rootLogger=ERROR, stdout, rollingFile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.File=${myWebapp-instance-root}/WEB-INF/logs/application.log log4j.appender.rollingFile.MaxFileSize=512KB log4j.appender.rollingFile.MaxBackupIndex=10 log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.rollingFile.Encoding=UTF-8
We will define "myWebapp-instance-root" later at (3)
2) Specify the configuration location in web.xml:
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j-myapp.properties</param-value> </context-param>
3) Provide a variable name unique to your webapp root, for example. "MyWebapp-instance-root"
<context-param> <param-name>webAppRootKey</param-name> <param-value>myWebapp-instance-root</param-value> </context-param>
4) Add Log4jConfigListener:
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
If you choose a different name, be sure to also change it in log4j-myapp.properties.
See my article (Italian only ... but it should be clear): http://www.megadix.it/content/configurare-path-relativi-log4j-utilizzando-spring
UPDATE (2009/08/01) I translated the article into English: http://www.megadix.it/node/136
Megadix Nov 26 '08 at 11:40 2008-11-26 11:40
source share