程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> log4j的配制及測試程序

log4j的配制及測試程序

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved