posts - 23,comments - 12,trackbacks - 0

1.把log4j-1.2.8.jar放到你的lib下,設置好classpath
2.添加配置文件.
配置文件取名log4j.properties,直接放到WEB-INF/classes下即可
例如:
log4j.rootLogger=DEBUG, ROOT

log4j.appender.ROOT=org.apache.log4j.RollingFileAppender
log4j.appender.ROOT.File=MMQB_Debug_Info.log
log4j.appender.ROOT.MaxFileSize=200KB
log4j.appender.ROOT.MaxBackupIndex=5
log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout
log4j.appender.ROOT.layout.ConversionPattern=[%d] %c %-5p - %m%n
#log4j.logger.com.webage.ejbs=INFO

3.在程序中
protected Log log = LogFactory.getLog(this.getClass().getName());
log.debug("debug infomation");


首先建立一个servlet
/*
 * 创建日期 2005-4-4
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package cn.wst.common.log4j;


 

/*
 * 创建日期 2005-3-8
 *
 * 源文件: MyLog4jInit.java
 */

 
import java.io.IOException;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.log4j.PropertyConfigurator;

/**
 * 用于初始化Log4j
 *
 * @author wst
 * @version 1.0
 */
public class MyLog4jInit extends HttpServlet {
 
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
         String prefix = getServletContext().getRealPath("/");
         String file = getInitParameter("log4j"); // if the log4j-init-file is ot
         System.out.println("--------  Log4J Start [AddressBook] --------- ");
         if ( file != null ) {
             DOMConfigurator.configure(prefix + file);
       //   PropertyConfigurator.configure(prefix+file);

         }
     }
 
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response) throws ServletException, IOException {
     }
 
 }
 
然后在web-inf里面的最开头配置servlet
 <servlet>
    <servlet-name>MyLog4jInit</servlet-name>
    <servlet-class>cn.wst.common.log4j.MyLog4jInit</servlet-class>
   <init-param>
    <param-name>log4j</param-name>
  <param-value>WEB-INF/log4j.xml</param-value>
  </init-param>
   <load-on-startup>1</load-on-startup><!--log4j的初始化-->
 </servlet>
相应在web-inf下的log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <appender name="cn.wst.file.log"
  class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="C:/addrLog.txt" />
  <param name="Append" value="false" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%C:%M()]-[%p] %m%n" />
  </layout>
 </appender>

 <appender name="cn.wst.console.log"
  class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%5p  (%F:%L) - %m%n" />
  </layout>
 </appender>
 <!--  use this to turn on debug to a rolling file. -->
 <root>
  <level value="info" />
  <appender-ref ref="cn.wst.console.log" />
  <appender-ref ref="cn.wst.file.log " />
 </root>
 
</log4j:configuration>

在程序中使用使用:假设类名为Find_hotel_servlet,则
先证明全局变量static Logger logger = Logger.getLogger(Find_hotel_servlet.class.getName());
然后使用只要Find_hotel_servlet.logger.info(*****)就可以了

posted on 2005-08-24 13:15 my java 阅读(291) 评论(0)  编辑  收藏 所属分类: java 转帖

只有注册用户登录后才能发表评论。


网站导航: