当前位置:Gxlcms > 数据库问题 > tomcat访问(access)日志配置、记录Post请求参数(转)

tomcat访问(access)日志配置、记录Post请求参数(转)

时间:2021-07-01 10:21:17 帮助过:48人阅读

%h   服务器的名称。如果resolveHosts为false的话,这里就是IP地址了,例如我的日志里是10.217.14.16 %H   访问者的协议,这里是HTTP/1.0 %l   官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns ‘-‘) %m   访问的方式,是GET还是POST %p   本地接收访问的端口  %q   比如你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,就是querystring的意思 %r   First line of the request (method and request URI) 请求的方法和URL %s   http的响应状态码  %S   用户的session ID,这个session ID大家可以另外查一下详细的解释,反正每次都会生成不同的session ID %t   请求时间 %u   得到了验证的访问者,否则就是"-" %U   访问的URL地址,我这里是/rightmainima/leftbott4.swf %v   服务器名称,可能就是你url里面写的那个吧,我这里是localhost %D   Time taken to process the request,in millis,请求消耗的时间,以毫秒记 %T   Time taken to process the request,in seconds,请求消耗的时间,以秒记

附:参考官方文档: http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html

 

二、配置打印POST参数

另外%r参数能打印出请求的url和get参数。如果url指定访问方式是post,post的参数是打印不出来的。当需要打印post参数,该怎么办?

大家注意到我开篇举例Valve配置里的%{postdata}r。没错,这个combined格式的patterrn可以实现。但是只在valve里配置这个东东还不够。因为postdata使我们自定义的参数名。需要在request中设置这个值。下面附上设置postdata到request中的代码。

  1. <span style="color: #0000ff">package</span><span style="color: #000000"> com.xiaoxiliu
  2. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException;
  3. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Enumeration;
  4. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.Filter;
  5. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterChain;
  6. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterConfig;
  7. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletException;
  8. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletRequest;
  9. </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletResponse;
  10. </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.slf4j.Logger;
  11. </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.slf4j.LoggerFactory;
  12. </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">class</span> PostDataDumperFilter <span style="color: #0000ff">implements</span><span style="color: #000000"> Filter {
  13. Logger logger </span>=<span style="color: #000000"> LoggerFactory.getLogger(getClass());
  14. </span><span style="color: #0000ff">private</span> FilterConfig filterConfig = <span style="color: #0000ff">null</span><span style="color: #000000">;
  15. </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> destroy() {
  16. </span><span style="color: #0000ff">this</span>.filterConfig = <span style="color: #0000ff">null</span><span style="color: #000000">;
  17. }
  18. </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  19. </span><span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, ServletException {
  20. </span><span style="color: #0000ff">if</span> (filterConfig == <span style="color: #0000ff">null</span><span style="color: #000000">)
  21. </span><span style="color: #0000ff">return</span><span style="color: #000000">;
  22. Enumeration</span><String> names =<span style="color: #000000"> request.getParameterNames();
  23. StringBuilder output </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder();
  24. </span><span style="color: #0000ff">while</span><span style="color: #000000"> (names.hasMoreElements()) {
  25. String name </span>=<span style="color: #000000"> (String) names.nextElement();
  26. output.append(name).append(</span>"="<span style="color: #000000">);
  27. String values[] </span>=<span style="color: #000000"> request.getParameterValues(name);
  28. </span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < values.length; i++<span style="color: #000000">) {
  29. </span><span style="color: #0000ff">if</span> (i > 0<span style="color: #000000">) {
  30. output.append(</span>"‘ "<span style="color: #000000">);
  31. }
  32. output.append(values[i]);
  33. }
  34. </span><span style="color: #0000ff">if</span><span style="color: #000000"> (names.hasMoreElements())
  35. output.append(</span>"&"<span style="color: #000000">);
  36. }
  37. request.setAttribute(</span>"postdata"<span style="color: #000000">, output);
  38. logger.debug(</span>"postdata: " +<span style="color: #000000"> output);
  39. chain.doFilter(request, response);
  40. }
  41. </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> init(FilterConfig filterConfig) <span style="color: #0000ff">throws</span><span style="color: #000000"> ServletException {
  42. </span><span style="color: #0000ff">this</span>.filterConfig =<span style="color: #000000"> filterConfig;
  43. }
  44. }</span>

在web.xml中添加配置该filter:

  1. <filter>
  2. <filter-name>post-data-dumper-filter</filter-name>
  3. <filter-<span style="color: #0000ff">class</span>>com.xiaoxiliu.PostDataDumperFilter</filter-<span style="color: #0000ff">class</span>>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>post-data-dumper-filter</filter-name>
  7. <url-pattern><span style="color: #008000">/*</span><span style="color: #008000"></url-pattern>
  8. </filter-mapping></span>

三、查询访问最耗时的接口

这就要用到万能的awk了 我们的日志倒数第二列显示的访问时间。cat logs/localhost_access_log.2016-10-25.txt | awk ‘{print $(NF-1)" "$0}‘ | sort -n -r| awk ‘{$1="";print $0}‘  按照倒数第二列由大到小显示接口以及访问时间。这样我们就能找出那些借口耗时较大,然后对其进行优化,提高用户体验。

 技术分享

人气教程排行