Tomcat AccessLog 格式化

來源:互聯網
上載者:User

標籤:stack   時間   val   format   port   http   格式化   end   director   

有的時候我們要使用日誌分析工具對日誌進行分析,需要對日誌進行格式化,比如,要把accessLog格式化成這樣的格式

c-ip s-ip x-InstancePort date time-taken x-Protocol cs-method cs-uri sc-status x-ResponseSize bytes x-Referrer x-UserAgent

在tomcat的conf/server.xml加入配置:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"               prefix="localhost_access_log" suffix=".txt"               pattern="%{X-Real-IP}i %A %p %{yyyy-MM-dd HH:mm:ss}t %D %H %m %U %s %B %B %{Referer}i %{User-Agent}i" />

關於accessLog的官方說明,請看 http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Logging 

常用配置如下:

  • %a - Remote IP address  --遠程IP地址
  • %A - Local IP address --本地IP地址
  • %b - Bytes sent, excluding HTTP headers, or ‘-‘ if zero --發送的位元組數(Bytes sent), 不包括HTTP headers的位元組,如果為0則展示‘-‘
  • %B - Bytes sent, excluding HTTP headers --發送的位元組數(Bytes sent), 不包括HTTP headers的位元組
  • %h - Remote host name (or IP address if enableLookups for the connector is false) --遠程主機名稱(如果resolveHosts為false則展示IP)
  • %H - Request protocol --請求協議
  • %l - Remote logical username from identd (always returns ‘-‘) --遠端使用者名,始終為‘-‘(Remote logical username from identd)
  • %m - Request method (GET, POST, etc.) --請求的方法(GET, POST等)
  • %p - Local port on which this request was received. See also %{xxx}p below. --接受請求的本地連接埠
  • %q - Query string (prepended with a ‘?‘ if it exists) --查詢字串,如果存在,有一個前置的‘?‘
  • %r - First line of the request (method and request URI) --請求的第一行(包括要求方法和請求的URI)
  • %s - HTTP status code of the response --response的HTTP狀態代碼(200,404等)
  • %S - User session ID --使用者的session ID
  • %t - Date and time, in Common Log Format --日期和時間,Common Log Format格式
  • %u - Remote user that was authenticated (if any), else ‘-‘ --被認證的遠端使用者, 不存在則展示‘-‘
  • %U - Requested URL path --請求URL路徑
  • %v - Local server name --本地服務名
  • %D - Time taken to process the request, in millis --處理請求的時間,單位為毫秒
  • %T - Time taken to process the request, in seconds --處理請求的時間,單位為秒
  • %F - Time taken to commit the response, in millis --提交響應的時間,以毫秒為單位
  • %I - Current request thread name (can compare later with stacktraces) --當前請求的線程名

Access Log中也支援cookie,請求header,響應headers,Session或者其他在ServletRequest中的對象的資訊

  • %{xxx}i write value of incoming header with name xxx
  • %{xxx}o write value of outgoing header with name xxx
  • %{xxx}c write value of cookie with name xxx
  • %{xxx}r write value of ServletRequest attribute with name xxx
  • %{xxx}s write value of HttpSession attribute with name xxx
  • %{xxx}p write local (server) port (xxx==local) or remote (client) port (xxx=remote)
  • %{xxx}t write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern xxx

如果tomcat前面有nginx做負載平衡,則要修改nginx的配置

Nginx的server主機配置段中添加:

proxy_set_header X-Real-IP $remote_addr; #真實的遠端IP地址

然後再tomcat accessLog用%{X-Real-IP}i 擷取即可

Tomcat AccessLog 格式化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.