Generate user access logs with logs and filters

xiaoxiao2021-03-06  39

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, Filterchain Chain) throws servletexception, ioException {httpservletRequest Req = (httpservletRequest) Request;

Chain.dofilter (Request, Response); // PropertyConfigurator.configure ("log4j.properties);

Logfilter.logger.info (req.getRemoteHost () "Trying to access" Req.getRequestURL () "in" "(report:" filtername ".)" );

//}

public void init (FilterConfig config) throws ServletException {this.config = config; // In case it is needed by subclass context = config.getServletContext ();. filterName = config.getFilterName ();}

Public void destroy () {}

} // Get the simple class 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, allowing users to access files, time, and users of the user when accessing JSP or servlets. Logger xzt.rs.tools.logfilter Logger / *. Jsp Logger / servlet / * - ... > Generated file content info [tcpConnection-8080-3] (logfilter.java: 31) - NewTransit Attempt to access http://192.168.0.3:8080/oasystem/main.jsp at 2005-01-21-16-40- 31. (report: logger.) Log4j.Properties profile log4j.rootlogger = debug, A1

# Print to the file

Log4j.Appender.a1 = org.apache.log4j.rollingfileappender

Log4j.Appender.a1.file = f: /resin/resin-2.0.4/bak/sdcdcbak.log

Log4j.Appender.a1.maxfilesize = 1024KB

Log4j.Appender.a1.maxbackupindex = 100

Log4j.Appender.a1.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.a1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

# Print to the console

# log4j.appender.b1 = org.apache.log4j.consoleappender

# log4j.appender.b1.layout = org.apache.log4j.patternlayout

# log4j.Appender.b1.Layout.conversionPattern =% - 4R% -5P [% T]% 37C% 3X -% M% N

# log4j.Appender.b1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

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

New Post(0)