java 日志过滤器

记录日志.

LoggerFilter.java

package com.easyweb.web.character;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * 全局日志记录过滤器(暂且不用,现在使用的是spring aop记录日志)
 * @author Administrator
 *
 */
public class LoggerFilter implements Filter{
  private FilterConfig filterConfig = null;

    public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)
      throws IOException, ServletException
    {

      long start = System.currentTimeMillis();
      String address =   request.getRemoteAddr();
      String file = ((HttpServletRequest) request).getRequestURI();
      chain.doFilter(request, response);

      filterConfig.getServletContext().log(
          "User access! " +     
          " User IP: " + address +     
          " Resource: " + file +
          " Milliseconds used: " + (System.currentTimeMillis() - start)
      );
      //记录到数据库
    }

    public void destroy() { }

    public void init(FilterConfig filterConfig) {
      this.filterConfig = filterConfig;
    }

}

你可能感兴趣的:(java)