第一步,
Properties文件(first_log4j.properties) 放在系统的src包中。(eclipse布置在"WEB-INF"classes)
log4j.rootLogger=INFO,stdout,logfile
log4j.category.com.sumit=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= [%p][%c]- %m [%d]
%n
# logfile set up.
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=set up in web.xml
log4j.appender.logfile.MaxFileSize=50MB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=10
# Pattern to output: date
priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout=org.apache.log4j.HTMLLayout
#log4j.appender.logfile.layout.ConversionPattern=时间:%d%n 优先级:%p%n 源文件:%F%n 类:%c%n 方法:%M%n 行数%L%n 最全:%l%n 信息:%m%n------------------------------------------------%n%n
log4j.appender.logfile.layout.ConversionPattern=%n- - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - -
- - - - - -- - - - - - - - - - - - - - - - - - - - %n时间:%d%n 优先级:%p%n 位置:%l%n 信息:%m%n%n
第二步,servlet
package com.royal.listener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
publicclass LogListener extends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
static Logger log = Logger.getLogger(LogListener.class);
public LogListener() {
}
publicvoid init(ServletConfig config) throws ServletException {
String prefix =
config.getServletContext().getRealPath("/");
String properties =
config.getInitParameter("log4j_properties");
String logger=config.getInitParameter("log4j_outPutFile");
String propertiesPath = prefix + properties;
Properties props = new Properties();
try {
FileInputStream istream = new
FileInputStream(propertiesPath);
props.load(istream);
istream.close();
//toPrint(props.getProperty("log4j.appender.file.File"));
String logFile = prefix + logger;//设置路径
logFile=logFile.replace('""', '/');
props.setProperty("log4j.appender.logfile.File",logFile);
PropertyConfigurator.configure(props);//装入log4j配置信息
log.info("皇家金典项目启动------");
log.info("LOG被加载日志文件位置:"+props.getProperty("log4j.appender.logfile.File"));
} catch (IOException ioe) {
log.error(ioe);
ioe.printStackTrace();
return;
}
}
}
Web.xml: (load-on-srartup=1 项目启动时加载)
参数中有一个是logs"log.log,自动生成日志文件,生成在项目下面的logs directory中,文件名为log.log
<servlet>
<servlet-name>LogListener</servlet-name>
<servlet-class>com.royal.listener.LogListener</servlet-class>
<init-param>
<param-name>log4j_properties</param-name>
<param-value>WEB-INF/classes/first_log4j.properties</param-value>
</init-param>
<init-param>
<param-name>log4j_outPutFile</param-name>
<param-value>logs"log.log</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
第三步 使用
如果是普通的JAVA类,
就得初始化log变量
static Logger log =
Logger.getLogger(Mail.class);
在要记录日去的地方
log.error(ex+" 创建MIME邮件对象失败!"); //ex是Exception对象
或者
log.info/log.debug/log.warn等
如果是struts中的DispatchAction的子类,或其它有Log的类的子类。
就可以不用初始化log变量。
posted on 2007-12-18 11:31
xyz 阅读(347)
评论(0) 编辑 收藏 所属分类:
他山之石