Log4J在Web应用中的使用

1、在web.xml中配置
<servlet>
      <servlet-name>log4j</servlet-name>
      <servlet-class>servlet.Log4JServlet</servlet-class>
      <init-param>
          <param-name>profile</param-name>
          <param-value>WEB-INF/log4j.properties</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
2、开发Log4JServlet
public class Log4JServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 2546022776565494776L;

    public void init() {
        String prefix = this.getServletContext().getRealPath("/");
        String file = this.getInitParameter("profile");
        if (file != null) {
            PropertyConfigurator.configure(prefix + file);
        }
    }
}
2、配置log4j.properties
log4j.rootLogger=debug, 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=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=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

posted on 2009-10-13 21:43  Hero King  阅读(280)  评论(0编辑  收藏  举报

导航