log4j日志文件怎么输出为相对路径?实例分析

发布时间:2019-12-29编辑:脚本学堂
有关log4j日志组件输出文件为相对路径的例子,二种方法实现log4j日志输出相对路径,需要的朋友参考下。

log4j日志组件用法

方法1,将日志文件输出到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

方法2,将日志文件输出到自己程序WEB-INFlogs目录中。
 

复制代码 代码示例:
<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-INFclasseslog4j.properties</param-value> 
   </init-param> 
   <init-param> 
<param-name>log4j_file_path</param-name> 
<param-value>WEB-INFlogs</param-value> 
    </init-param> 
   <load-on-startup>0</load-on-startup> 
</servlet> 

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

复制代码 代码示例:
package com.linder.util.log4j.init;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.util.Properties;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.apache.log4j.Logger;  
import org.apache.log4j.PropertyConfigurator;  
public class Lo4jInit   extends HttpServlet {  
/** 
 *  
*/ 
private static final long serialVersionUID = 1L;  
private static Logger logger=Logger.getLogger(Lo4jInit.class);  
public void init(){  
String path=this.getServletContext().getRealPath("/");  
String file=this.getInitParameter("log4j_init_path");  
String logFile=this.getInitParameter("log4j_file_path");  
if(file!=null)  
 {  
 Properties prop = new Properties();   
 try{  
 prop.load(new FileInputStream(path+file)); //加载log4j.properties  
 prop.setProperty("log4j.appender.R.File", path+logFile+ prop.getProperty("log4j.appender.R.File")); //设置日志文件的输出路径  
 PropertyConfigurator.configure(prop); //加载配置项  
 }catch(Exception e)  
{  
logger.info("初始化log4j日志输入路径异常,请检查web.xml参数配置是否正常,异常发生在"+this.getClass().getName()+"类的public void init()方法,异常的愿意是:"+e.getMessage(), e.fillInStackTrace());  
  }  
  }  
 }  
protected void service(HttpServletRequest request, HttpServletResponse response)  
 throws ServletException, IOException {  
}  

3、log4j.properties的配置信息
 

复制代码 代码示例:
# Set root category priority to info and its only appender to console.   
log4j.rootCategory=info,console,R  
#log4j.debug=true 
 
# 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=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