方法一:直接在log4j配置中写绝对路劲
方法二:新建一个ServletContextListener的实现类Log4jConfigListener,Log4jConfigListener做:
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配置中:
log4j.appender.A2.File=${webapp}/logs/log.log
方法三(跟Spring集成):在web.xml中配置
webAppRootKey
webapp.root
log4jConfigLocation
/WEB-INF/config/log4j.properties
log4jRefreshInterval
6000
org.springframework.web.util.Log4jConfigListener
然后在log4j中用上述配置的webAppRootKey对应的值
log4j.appender.file.File=${webapp.root}/WEB-INF/logs/log.log