log4j路径配置

Log4j路径配置:绝对路径、使用Listener、spring集成(配置web.xml)。

  1. 方法一

直接在log4j配置中写绝对路径

  1. 方法二

新建一个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  

也可以使用jvm里自带的参数例如user.home

  1. 方法三

(跟Spring集成):在web.xml中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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对应的值