点击这里给我发消息 点击这里给我发消息

技巧分享——log4j的配制及测试程序

添加时间:2013-12-7
    相关阅读: WEB 程序

  1,写个初始化的SERVLET:
  
  package net.welovetennis.util.log;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  import org.apache.log4j.*;
  
  /**
  * <p>Title: </p>
  * <p>Description: </p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: </p>
  * @author not attributable
  * @version 1.0
  */
  
  public class Log4jInit extends HttpServlet {
  
  //Initialize global variables
  public void init() throws ServletException {
  String prefix = getServletContext().getRealPath("/");
  String file = getInitParameter("log4j");
  // if the log4j-init-file is not set, then no point in trying
  System.out.println("................log4j start");
  if(file != null) {
  PropertyConfigurator.configure(prefix+file);
  }
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
  
  }
  //Clean up resources
  public void destroy() {
  }
  }
  
  2,修改WEB。XML:
  
  <servlet>
  <servlet-name>log4jinit</servlet-name>
  <servlet-class>net.welovetennis.util.log.Log4jInit</servlet-class>
  <init-param>
  <param-name>log4j</param-name>
  <param-value>WEB-INF/log4j.properties</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  3,在WEB-INF下放配制文件log4j.properties,并指定输出路径:
  
  log4j.rootLogger=INFO, A1 , R
  log4j.appender.A1=org.apache.log4j.ConsoleAppender
  log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=c:/project/david/racquet/logs/log4j.log
  log4j.appender.R.MaxFileSize=100KB
  log4j.appender.R.MaxBackupIndex=1
  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册