时间:2021-07-01 10:21:17 帮助过:48人阅读
附:参考官方文档: 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中的代码。
- <span style="color: #0000ff">package</span><span style="color: #000000"> com.xiaoxiliu
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Enumeration;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.Filter;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterChain;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterConfig;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletException;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletRequest;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletResponse;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.slf4j.Logger;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.slf4j.LoggerFactory;
- </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 {
- Logger logger </span>=<span style="color: #000000"> LoggerFactory.getLogger(getClass());
- </span><span style="color: #0000ff">private</span> FilterConfig filterConfig = <span style="color: #0000ff">null</span><span style="color: #000000">;
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> destroy() {
- </span><span style="color: #0000ff">this</span>.filterConfig = <span style="color: #0000ff">null</span><span style="color: #000000">;
- }
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- </span><span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, ServletException {
- </span><span style="color: #0000ff">if</span> (filterConfig == <span style="color: #0000ff">null</span><span style="color: #000000">)
- </span><span style="color: #0000ff">return</span><span style="color: #000000">;
- Enumeration</span><String> names =<span style="color: #000000"> request.getParameterNames();
- StringBuilder output </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder();
- </span><span style="color: #0000ff">while</span><span style="color: #000000"> (names.hasMoreElements()) {
- String name </span>=<span style="color: #000000"> (String) names.nextElement();
- output.append(name).append(</span>"="<span style="color: #000000">);
- String values[] </span>=<span style="color: #000000"> request.getParameterValues(name);
- </span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < values.length; i++<span style="color: #000000">) {
- </span><span style="color: #0000ff">if</span> (i > 0<span style="color: #000000">) {
- output.append(</span>"‘ "<span style="color: #000000">);
- }
- output.append(values[i]);
- }
- </span><span style="color: #0000ff">if</span><span style="color: #000000"> (names.hasMoreElements())
- output.append(</span>"&"<span style="color: #000000">);
- }
- request.setAttribute(</span>"postdata"<span style="color: #000000">, output);
- logger.debug(</span>"postdata: " +<span style="color: #000000"> output);
- chain.doFilter(request, response);
- }
- </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 {
- </span><span style="color: #0000ff">this</span>.filterConfig =<span style="color: #000000"> filterConfig;
- }
- }</span>
在web.xml中添加配置该filter:
- <filter>
- <filter-name>post-data-dumper-filter</filter-name>
- <filter-<span style="color: #0000ff">class</span>>com.xiaoxiliu.PostDataDumperFilter</filter-<span style="color: #0000ff">class</span>>
- </filter>
- <filter-mapping>
- <filter-name>post-data-dumper-filter</filter-name>
- <url-pattern><span style="color: #008000">/*</span><span style="color: #008000"></url-pattern>
- </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}‘ 按照倒数第二列由大到小显示接口以及访问时间。这样我们就能找出那些借口耗时较大,然后对其进行优化,提高用户体验。