Log4j日誌文件存放到項目目錄下

方法一:直接在log4j配置中寫絕對路勁html


方法二:新建一個ServletContextListener的實現類Log4jConfigListener,Log4jConfigListener作:java

[java] view plaincopy在CODE上查看代碼片派生到個人代碼片web

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 plaincopy在CODE上查看代碼片派生到個人代碼片app

log4j.appender.A2.File=${webapp}/logs/log.log



方法三(跟Spring集成):在web.xml中配置webapp

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片spa

<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 plaincopy在CODE上查看代碼片派生到個人代碼片code

log4j.appender.file.File=${webapp.root}/WEB-INF/logs/log.log
相關文章
相關標籤/搜索