Tomcat的访问日志-localhost_access_log和记录Post请求参数

来源:互联网 发布:电玩游戏平台源码 编辑:程序博客网 时间:2024/05/17 22:36
  1. tomcat的日志分类
    1. http://blog.csdn.net/fly910905/article/details/78463909

  2. tomcat产生的访问日志数据  【localhost_access_log.Y-M-D.txt】
    1. 它记录的访问的时间,IP,访问的资料等相关信息

  3.  首先是配置tomcat访问日志数据,配置的方式如下
    1. 打开${catalina}/conf/server.xml文件
    2. 注:${catalina}是tomcat的安装目录
      1. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b"
      2. prefix="localhost_access_log" suffix=".txt"/>


      tomcat访问日志关闭:
      1. 只需要将
      2. ${catalina}/conf/server.xml里面的下列申明注释掉再重启tomcat就好

    参数说明:

    className       官方文档上说了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。directory日志文件存放的目录。通常设置为tomcat下已有的那个logs文件。prefix日志文件的名称前缀。suffix日志文件的名称后缀。pattern最主要的参数。
    pattern。它的参数比较多。可以设置成common,combined两种格式。

    common的值:%h %l %u %t %r %s %b
    combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i   

    resolveHosts如果是true,tomcat会将这个服务器IP地址通过DNS转换为主机名;如果是false,就直接写服务器IP地址。默认false。rotatable 默认为true,tomcat生成的文件名为prefix(前缀)+.+时间(一般是按天算)+.+suffix(后缀),如:localhost_access_log.2007-09-22.txt。设置为false的话,tomcat会忽略时间,不会生成新文件,文件名就是:localhost_access_log.txt。长此以往,这个日志文件会超级大condition这个参数不太实用,可设置任何值,比如设置成condition="tkq",那么只有当ServletRequest.getAttribute("tkq")为空的时候,该条日志才会被记录下来。
    fileDateFormat                                                                                 
    顾名思义,就是时间格式嘛。但这个时间格式是针对日志文件名起作用的。咱们生成的日志文件全名:localhost_access_log.2016-09-22.txt,这里面的2016-09-22就是这么来的。如果想让tomcat每小时生成一个日志文件,也很简单,将这个值设置为:fileDateFormat="yyyy-MM-dd.HH",当然也可以按分钟生成什么的,自己改改吧^_^
  4.  具体的日志产生样式说明如下(从官方文档中摘录)
    1. * %a - Remote IP address // 这是记录访问者的IP,在日志里是127.0.0.1
    2. * %A - Local IP address // 这是记录本地服务器的IP,在日志里是192.168.254.108
    3. * %b - Bytes sent, excluding HTTP headers, or '-' if zero // 发送信息的字节数,不包括http头,如果字节数为0的话,显示为-
    4. * %B - Bytes sent, excluding HTTP headers // 发送信息的字节数,不包括http头。
    5. * %h - Remote host name (or IP address if resolveHosts is false) //服务器的名称。如果resolveHosts为false的话,这里就是IP地址了,例如我的日志里是10.217.14.16
    6. * %H - Request protocol //访问者的协议,这里是HTTP/1.0
    7. * %l - Remote logical username from identd (always returns '-') // 官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns '-')
    8. * %m - Request method (GET, POST, etc.) // 访问的方式,是GET还是POST
    9. * %p - Local port on which this request was received // 本地接收访问的端口
    10. * %q - Query string (prepended with a '?' if it exists) // 比如你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,就是querystring的意思
    11. * %r - First line of the request (method and request URI) // First line of the request (method and request URI) 请求的方法和URL
    12. * %s - HTTP status code of the response // http的响应状态码
    13. * %S - User session ID // 用户的session ID,这个session ID大家可以另外查一下详细的解释,反正每次都会生成不同的session ID
    14. * %t - Date and time, in Common Log Format // 请求时间
    15. * %u - Remote user that was authenticated (if any), else '-' // 得到了验证的访问者,否则就是"-"
    16. * %U - Requested URL path // 访问的URL地址,我这里是/rightmainima/leftbott4.swf
    17. * %v - Local server name // 服务器名称,可能就是你url里面写的那个吧,我这里是localhost
    18. * %D - Time taken to process the request, in millis // 请求消耗的时间,以毫秒记
    19. * %T - Time taken to process the request, in seconds //请求消耗的时间,以秒记
    20. There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.
    21. It is modeled after the apache syntax:
    22. * %{xxx}i for incoming headers
    23. * %{xxx}c for a specific cookie
    24. * %{xxx}r xxx is an attribute in the ServletRequest
    25. * %{xxx}s xxx is an attribute in the HttpSession


  5.  示例【格式为 %h %l %u %t "%r" %s %b
    1. 140.205.201.39 - - [31/Aug/2017:17:06:52 +0800] "GET /clusters.jsf HTTP/1.1" 404 -
    2. 60.191.38.77 - - [31/Aug/2017:17:11:35 +0800] "GET / HTTP/1.1" 404 -
    3. 61.232.253.54 - - [31/Aug/2017:19:04:53 +0800] "GET / HTTP/1.1" 404 -
    4. 100.116.151.190 - - [31/Aug/2017:20:45:17 +0800] "GET /CloudPayment/bill/querySelfPayList.do HTTP/1.0" 200 60
    5. 100.116.41.2 - - [31/Aug/2017:20:45:34 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    6. 100.116.151.145 - - [31/Aug/2017:20:46:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    7. 100.116.41.41 - - [31/Aug/2017:20:46:42 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    8. 100.116.151.181 - - [31/Aug/2017:20:47:21 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422
    9. 100.116.151.181 - - [31/Aug/2017:20:47:22 +0800] "GET /CloudPayment/user/pay.do HTTP/1.0" 200 1422

  6.  记录Post请求参数
    1. 另外%r参数能打印出请求的urlget参数。如果url指定访问方式是post,post的参数是打印不出来的。当需要打印post参数,该怎么办?
    server.xml配置
    1. tomcat访问日志格式配置,在config/server.xmlHost标签下加上
    2. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    3. prefix="localhost_access_log." suffix=".txt"
    4. pattern="%h %l %u %t &quot;%r&quot; [%{postdata}r] %s %{Referer}i %{User-Agent}i %T %b" />
    postdata过滤器
    1. package com.xiaoxiliu
    2. import java.io.IOException;
    3. import java.util.Enumeration;
    4. import javax.servlet.Filter;
    5. import javax.servlet.FilterChain;
    6. import javax.servlet.FilterConfig;
    7. import javax.servlet.ServletException;
    8. import javax.servlet.ServletRequest;
    9. import javax.servlet.ServletResponse;
    10. import org.slf4j.Logger;
    11. import org.slf4j.LoggerFactory;
    12. public final class PostDataDumperFilter implements Filter {
    13. Logger logger = LoggerFactory.getLogger(getClass());
    14. private FilterConfig filterConfig = null;
    15. public void destroy() {
    16. this.filterConfig = null;
    17. }
    18. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    19. throws IOException, ServletException {
    20. if (filterConfig == null)
    21. return;
    22. Enumeration<String> names = request.getParameterNames();
    23. StringBuilder output = new StringBuilder();
    24. while (names.hasMoreElements()) {
    25. String name = (String) names.nextElement();
    26. output.append(name).append("=");
    27. String values[] = request.getParameterValues(name);
    28. for (int i = 0; i < values.length; i++) {
    29. if (i > 0) {
    30. output.append("' ");
    31. }
    32. output.append(values[i]);
    33. }
    34. if (names.hasMoreElements())
    35. output.append("&");
    36. }
    37. request.setAttribute("postdata", output);
    38. logger.debug("postdata: " + output);
    39. chain.doFilter(request, response);
    40. }
    41. public void init(FilterConfig filterConfig) throws ServletException {
    42. this.filterConfig = filterConfig;
    43. }
    44. }
    在web.xml中添加配置该filter
    1. <filter>
    2. <filter-name>post-data-dumper-filter</filter-name>
    3. <filter-class>com.xiaoxiliu.PostDataDumperFilter</filter-class>
    4. </filter>
    5. <filter-mapping>
    6. <filter-name>post-data-dumper-filter</filter-name>
    7. <url-pattern>/*</url-pattern>
    8. </filter-mapping>

参考来源:http://blog.csdn.net/qq_30121245/article/details/52861935

原创粉丝点击