熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

技巧分享——log4j的配制及測試程序

2022-06-13   來源: Java開源技術 

  寫個初始化的SERVLET
  
  package netwelovetennisutillog;
  
  import javaxservlet*;
  import javaxservlethttp*;
  import orgapachelogj*;
  
  /**
  * <p>Title: </p>
  * <p>Description: </p>
  * <p>Copyright: Copyright (c) </p>
  * <p>Company: </p>
  * @author not attributable
  * @version
  */
  
  public class LogjInit extends HttpServlet {
  
  //Initialize global variables
  public void init() throws ServletException {
  String prefix = getServletContext()getRealPath(/);
  String file = getInitParameter(logj);
  // if the logjinitfile is not set then no point in trying
  Systemoutprintln(logj start);
  if(file != null) {
  Propnfigure(prefix+file);
  }
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request HttpServletResponse response) {
  
  }
  //Clean up resources
  public void destroy() {
  }
  }
  
  修改WEBXML
  
  <servlet>
  <servletname>logjinit</servletname>
  <servletclass>netwelovetennisutillogLogjInit</servletclass>
  <initparam>
  <paramname>logj</paramname>
  <paramvalue>WEBINF/logjproperties</paramvalue>
  </initparam>
  <loadonstartup></loadonstartup>
  </servlet>
  
  在WEBINF下放配制文件logjproperties並指定輸出路徑
  
  logjrootLogger=INFO A R
  logjappenderA=orgapachelogjConsoleAppender
  logjappenderAlayout=orgapachelogjPatternLayout
  logjappenderAlayoutConversionPattern=%d{yyyyMMdd HH:mm:ss} [%c][%p] %m%n
  logjappenderR=orgapachelogjRollingFileAppender
  logjappenderRFile=c:/project/david/racquet/logs/logjlog
  logjappenderRMaxFileSize=KB
  logjappenderRMaxBackupIndex=
  logjappenderRlayout=orgapachelogjPatternLayout
  logjappenderRlayoutConversionPattern=%p %t %c %m%n
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28858.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.