当前位置: 代码迷 >> Web前端 >> Log4j日志文件输出为相对路径的解决办法
  详细解决方案

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

热度:353   发布时间:2012-11-12 12:31:57.0
Log4j日志文件输出为相对路径的解决方法

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

?

# 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=../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目录中

?

1、在web.xml配置一个servlet

<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

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

?

?

?

  相关解决方案