天天看點

java tomcat 日志分析工具_設計一個Tomcat通路日志分析工具

常使用web伺服器的朋友大都了解,一般的web server有兩部分日志:

一是運作中的日志,它主要記錄運作的一些資訊,尤其是一些異常錯誤日志資訊

二是通路日志資訊,它記錄的通路的時間,IP,通路的資料等相關資訊。

現在我來和大家介紹一下利用tomcat産生的通路日志資料,我們能做哪些有效的分析資料?

首先是配置tomcat通路日志資料,預設情況下通路日志沒有打開,配置的方式如下:

編輯 ${catalina}/conf/server.xml檔案.注:${catalina}是tomcat的安裝目錄

把以下的注釋()去掉即可。

其中 directory是産生的目錄 tomcat安裝${catalina}作為目前目錄

pattern表示日志生産的格式,common是tomcat提供的一個标準設定格式。其具體的表達式為 %h %l %u %t "%r" %s %b

但本人建議采用以下具體的配置,因為标準配置有一些重要的日志資料無法生。

%h %l %u %t "%r" %s %b %T

具體的日志産生樣式說明如下(從官方文檔中摘錄):

* %a -Remote IP address* %A -Local IP address* %b - Bytes sent, excluding HTTP headers, or '-' ifzero* %B -Bytes sent, excluding HTTP headers* %h - Remote host name (or IP address if resolveHosts is false)* %H -Request protocol* %l - Remote logical username from identd (always returns '-')* %m -Request method (GET, POST, etc.)* %p - Local port on which thisrequest was received* %q - Query string (prepended with a '?' ifit exists)* %r -First line of the request (method and request URI)* %s -HTTP status code of the response* %S -User session ID* %t -Date and time, in Common Log Format* %u - Remote user that was authenticated (if any), else '-'

* %U -Requested URL path* %v -Local server name* %D -Time taken to process the request, in millis* %T - Time taken to process the request, in seconds

There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest. It is modeled after the apache syntax:

* %{xxx}i forincoming headers* %{xxx}c fora specific cookie* %{xxx}r xxx is an attribute in the ServletRequest* %{xxx}s xxx is an attribute in the HttpSession

現在我們回頭再來看一下下面這個配置 %h %l %u %t "%r" %s %b %T 生産的通路日志資料,我們可以做哪些事?

先看一下,我們能得到的資料有:

* %h 通路的使用者IP位址* %l 通路邏輯使用者名,通常傳回'-'

* %u 通路驗證使用者名,通常傳回'-'

* %t 通路日時* %r 通路的方式(post或者是get),通路的資源和使用的http協定版本* %s 通路傳回的http狀态* %b 通路資源傳回的流量* %T 通路所使用的時間

有了這些資料,我們可以根據時間段做以下的分析處理(圖檔使用jfreechart工具動态生成):

*獨立IP數統計*通路請求數統計*通路資料檔案數統計*通路流量統計*通路處理響應時間統計*統計所有404錯誤頁面*統計所有500錯誤的頁面*統計通路最頻繁頁面*統計通路處理時間最久頁面* 統計并發通路頻率最高的頁面

分析工具包括兩大部分,一個是背景解釋程式,每天執行一次對背景日志資料進行解析後儲存到資料庫中。

第二個是顯示程式,從資料庫中查詢資料并生成相應的圖表資訊。