Log4j日志文件输出为相对路径的解决方法

方法一:将日志文件输出到tomcat自身的日志文件夹logs中

# console is set to be a ConsoleAppender using a PatternLayout. 
log4j.appender.console=org.apache.log4j.ConsoleAppender 
log4j.appender.console.Threshold=info 
log4j.appender.console.layout=org.apache.log4j.PatternLayout 
log4j.appender.console.layout.ConversionPattern=- %m%n

# R is set to be a File appender using a PatternLayout.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.Append=true
log4j.appender.R.Threshold=info 
log4j.appender.R.MaxFileSize=1024KB 
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.File=../logs/abc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] %m%n 

 

方法二:将日志文件输出到自己程序WEB-INF\logs目录中

 

<servlet>  
    <servlet-name>lo4jInit</servlet-name>  
    <servlet-class>com.linder.util.log4j.init.Lo4jInit</servlet-class>  
    <init-param>  
        <param-name>log4j_init_path</param-name>  
        <param-value>WEB-INF\classes\log4j.properties</param-value>  
   </init-param>  
    <init-param>  
        <param-name>log4j_file_path</param-name>  
       <param-value>WEB-INF\logs\</param-value>  
    </init-param>  
   <load-on-startup>0</load-on-startup>  
 </servlet>  

 

2、编写一个初始化log4j的Servlet

1.package com.linder.util.log4j.init;   
2.  
3.import java.io.FileInputStream;   
4.import java.io.IOException;   
5.import java.util.Properties;   
6.  
7.import javax.servlet.ServletException;   
8.import javax.servlet.http.HttpServlet;   
9.import javax.servlet.http.HttpServletRequest;   
10.import javax.servlet.http.HttpServletResponse;   
11.  
12.import org.apache.log4j.Logger;   
13.import org.apache.log4j.PropertyConfigurator;   
14.  
15.  
16.public class Lo4jInit   extends HttpServlet {   
17.  
18.    /**  
19.     *   
20.     */  
21.    private static final long serialVersionUID = 1L;   
22.    private static Logger logger=Logger.getLogger(Lo4jInit.class);   
23.       
24.    public void init(){   
25.        String path=this.getServletContext().getRealPath("/");   
26.        String file=this.getInitParameter("log4j_init_path");   
27.        String logFile=this.getInitParameter("log4j_file_path");   
28.        if(file!=null)   
29.        {   
30.            Properties prop = new Properties();    
31.            try{   
32.                prop.load(new FileInputStream(path+file)); //加载log4j.properties   
33.                prop.setProperty("log4j.appender.R.File", path+logFile+ prop.getProperty("log4j.appender.R.File")); //设置日志文件的输出路径   
34.                PropertyConfigurator.configure(prop); //加载配置项   
35.            }catch(Exception e)   
36.            {   
37.                logger.info("初始化log4j日志输入路径异常,请检查web.xml参数配置是否正常,异常发生在"+this.getClass().getName()+"类的public void init()方法,异常的愿意是:"+e.getMessage(), e.fillInStackTrace());   
38.            }   
39.        }   
40.           
41.  
42.    }   
43.    protected void service(HttpServletRequest request, HttpServletResponse response)   
44.    throws ServletException, IOException {   
45.           
46.    }   
47.  
48.}  

 

3、log4j.properties的配置信息

 

1.# Set root category priority to info and its only appender to console.    
2.log4j.rootCategory=info,console,R   
3.#log4j.debug=true  
4.  
5.# console is set to be a ConsoleAppender using a PatternLayout.    
6.log4j.appender.console=org.apache.log4j.ConsoleAppender    
7.log4j.appender.console.Threshold=info    
8.log4j.appender.console.layout=org.apache.log4j.PatternLayout    
9.log4j.appender.console.layout.ConversionPattern=- %m%n   
10.  
11.# R is set to be a File appender using a PatternLayout.   
12.log4j.appender.R=org.apache.log4j.RollingFileAppender   
13.log4j.appender.R.Append=true  
14.log4j.appender.R.Threshold=info    
15.log4j.appender.R.MaxFileSize=1024KB    
16.log4j.appender.R.MaxBackupIndex=10  
17.log4j.appender.R.File=abc.log   
18.log4j.appender.R.layout=org.apache.log4j.PatternLayout   
19.log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] %m%n 
 

 

你可能感兴趣的:(apache,tomcat,log4j,Web,servlet)