方法一:直接在log4j配置中寫絕對路勁html
方法二:新建一個ServletContextListener的實現類Log4jConfigListener,Log4jConfigListener作:java
[java] view plaincopyweb
String path = Environment.class.getResource("").getPath(); String webAppPath = path.substring(0, path.toUpperCase().lastIndexOf("WEB-INF/")).replaceAll("%20", " "); System.setProperty("webapp",webAppPath + "logs/log.log");
在log4j配置中:spring
[plain] view plaincopyapp
log4j.appender.A2.File=${webapp}/logs/log.log
方法三(跟Spring集成):在web.xml中配置webapp
[html] view plaincopyspa
<context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/config/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
而後在log4j中用上述配置的webAppRootKey對應的值
.net
[plain] view plaincopycode
log4j.appender.file.File=${webapp.root}/WEB-INF/logs/log.log