tomcat訪問(access)日志配置、記錄Post請求參數(轉)


一、配置與說明

tomcat訪問日志格式配置,在config/server.xml里Host標簽下加上

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; [%{postdata}r] %s %{Referer}i %{User-Agent}i %T %b" />

我們在日志文件中將看到如下文本:

10.217.14.16 - - [21/Oct/2016:15:48:54 +0800] "POST /updates/related_notice_num.json HTTP/1.0" [channel=App Store&gid=92391918-2173-4A66-8B31-B2FB3F8FB3DF&os=2&plat=2&sver=10.000000&token=MzM1OTc0MjQ1MkB3ZWliby55bXguY29tfHdlaWJvfDQ5ZGFmMjk0YjQ5YWQxMTZiZjBmYWM4ZDdhYzg3ZWQ0&ua=&ver=4.2.1] 200 - AllApp/4.2.1 (iPhone; iOS 10.0.2; Scale/3.00) 0.004 91

 

參數說明: 

className        官方文檔上說了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。
directory 日志文件存放的目錄。通常設置為tomcat下已有的那個logs文件。
prefix 日志文件的名稱前綴。
suffix 日志文件的名稱后綴。
pattern 最主要的參數。下面會細講。
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",當然也可以按分鍾生成什么的,自己改改吧^_^

 

 

下面着重講下pattern。它的參數比較多。可以設置成common,combined兩種格式。

common的值:%h %l %u %t %r %s %b
combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i        (至於combined的值的最后兩個為什么會這樣,我也不太清楚)

%a   這是記錄訪問者的IP,在日志里是127.0.0.1
%A   這是記錄本地服務器的IP,在日志里是192.168.254.108
%b   發送信息的字節數,不包括http頭,如果字節數為0的話,顯示為-
%B   發送信息的字節數,不包括http頭。
%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中的代碼。

package com.xiaoxiliu

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class PostDataDumperFilter implements Filter {

    Logger logger = LoggerFactory.getLogger(getClass());

    private FilterConfig filterConfig = null;

    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (filterConfig == null)
            return;

        Enumeration<String> names = request.getParameterNames();
        StringBuilder output = new StringBuilder();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            output.append(name).append("=");
            String values[] = request.getParameterValues(name);
            for (int i = 0; i < values.length; i++) {
                if (i > 0) {
                    output.append("' ");
                }

                output.append(values[i]);
            }
            if (names.hasMoreElements())
                output.append("&");
        }
        request.setAttribute("postdata", output);
        logger.debug("postdata: " + output);
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
}

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

<filter>
        <filter-name>post-data-dumper-filter</filter-name>
        <filter-class>com.xiaoxiliu.PostDataDumperFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>post-data-dumper-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

三、查詢訪問最耗時的接口

這就要用到萬能的awk了 我們的日志倒數第二列顯示的訪問時間。cat logs/localhost_access_log.2016-10-25.txt | awk '{print $(NF-1)" "$0}' | sort -n -r| awk '{$1="";print $0}'  按照倒數第二列由大到小顯示接口以及訪問時間。這樣我們就能找出那些借口耗時較大,然后對其進行優化,提高用戶體驗。

 

原文地址:

http://blog.csdn.net/qq_30121245/article/details/52861935


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM