Generate User Access Logs with Logs and Filters Select Blog from Lijunjiejava

xiaoxiao2021-03-06  40

Use log4j to generate a log. The log class is then added to the filter. Make the log when the user is accessible when the JSP or Servlet is asked. Easy to debug. Below is the address of the user access to the user and writes the file with log4j. Package xzt.rs.tools; import xzt.rs.tools.cundate; import java.io. *; import javax.servlet. *; import javax.servlet.http. *; import java.util. *; // for Date classimport org.apache.log4j *;. / ** Simple filter that prints a report in the log file * whenever the associated servlets or JSP pages * are accessed * / public class LogFilter implements filter {protected FilterConfig config;. private ServletContext context; private String filterName; private Cundate cd = new Cundate (); // static Logger logger = Logger.getLogger (LogFilter.class.getName ()); // public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {HttpServletRequest req = (HttpServletRequest) request; chain.doFilter (request, response); // PropertyConfigurator.configure ( "Log4j.properties"); LogFilter.logger.info (req.getRemoteHost () "attempts to access the" req .getRequestURL () "In" cd.gettime4 () "." "(Report:" filtername ".)") "); //} public void init (filterconfig) throws servletexception {this.config = config; // In Case It Is Needed by Subclass. Context = config.getServletContext (); FilterName = config.getfilterName ();} public void design {}} // Get the current date of the current date.

Package xzt.rs.tools. *; public class cupate {public string gettime4 () {date d = new date (); // get current date to d. SimpleDateFormat FM = New SimpleDateFormat ("YYYY-MM-DD-HH- MM-SS "); // set date format string strdate = fm.format (d); // get date to string strdate via format fm. Return strdate;}} Configuring a filter in Web.xml, making users access JSP Or write the file, time, and user of the user, write the log when servlet.

转载请注明原文地址:https://www.9cbs.com/read-64817.html

New Post(0)