tomcat8的access日志量很大,前端時(shí)間還嫌棄它,沒想到現(xiàn)在要依靠它來(lái)定位問題。
配置文件:$TOMCAT_HOME/conf/server.xml
默認(rèn)配置:%h %l %u %t "%r" %s %b"
日志顯示成:ip - - [12/Jan/2017:10:05:23 +0800] "GET url HTTP/1.1" 200 15846
因從客戶到服務(wù)端有個(gè)負(fù)載均衡,當(dāng)前%h 顯示的都是負(fù)載均衡地址,這對(duì)定位問題來(lái)說(shuō)一點(diǎn)用處也沒呀。
%h 改成
%a - Remote IP address
%A - Local IP address
目的: 用戶IP是多少,用戶訪問落到哪臺(tái)服務(wù)器上。
%l - Remote logical username from identd (always returns '-')
保留不動(dòng)
%u - Remote user that was authenticated (if any), else '-'
保留不動(dòng)
%t - Date and time, in Common Log Format
保留不動(dòng)
%r - First line of the request (method and request URI)
保留不動(dòng)(定位問題)
%s - HTTP status code of the response
保留不動(dòng) (定位問題)
%b - Bytes sent, excluding HTTP headers, or '-' if zero
保留不動(dòng)
增加:
%D - Time taken to process the request, in millis
請(qǐng)求消耗時(shí)間,單位毫秒 (定位問題)
%F - Time taken to commit the response, in millis
請(qǐng)求響應(yīng)的處理時(shí)間,單位毫秒 (定位問題)
日志配置:%a - %A %l %u %t "%r" %s %b request:%D response:%F
單機(jī)訪問的顯示日志:
10.0.6.240 - localip - - [12/Jan/2017:10:25:44 +0800] "GET url HTTP/1.1" 404 12698 request:63 response:22
10.0.6.240 - localip - - [12/Jan/2017:10:25:49 +0800] "GET url HTTP/1.1" 200 21102 request:168 response:159```
但是通過(guò)負(fù)載訪問時(shí),%a得到 還是 負(fù)載服務(wù)器的IP
參考Nginx的配置,日志配置:
%{X-Forwarded-For}i - %a -%A %l %u %t "%r" %s %b request:%D response:%F
當(dāng)然,如果還需要額外的信息怎么辦?比如 userID等,如果該信息和X-Forwarded-For一樣是頭文件中的,也可通過(guò)
%{UserID}i
**%{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
因日期格式比較難懂,日志配置改成:
pattern="%{X-Forwarded-For}i %{UserID}i %a %A %l %u %{yyyy-MM-dd HH:mm:ss}t "%r" %s %b %D %F" />
日期顯示成:2017-01-12 12:12:34
搞定。
>X-Forwarded-For 是一個(gè) HTTP 擴(kuò)展頭部。HTTP/1.1(RFC 2616)協(xié)議并沒有對(duì)它的定義,它最開始是由 Squid 這個(gè)緩存代理軟件引入,用來(lái)表示 HTTP 請(qǐng)求端真實(shí) IP。如今它已經(jīng)成為事實(shí)上的標(biāo)準(zhǔn),被各大 HTTP 代理、負(fù)載均衡等轉(zhuǎn)發(fā)服務(wù)廣泛使用,并被寫入 [RFC 7239](http://tools.ietf.org/html/rfc7239)(Forwarded HTTP Extension)標(biāo)準(zhǔn)之中。
獲取客戶端IP的代碼
var http = require('http');http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('remoteAddress: ' + req.connection.remoteAddress + '\n');
res.write('x-forwarded-for: ' + req.headers['x-forwarded-for'] + '\n');
res.write('x-real-ip: ' + req.headers['x-real-ip'] + '\n');
res.end();}).listen(8080, '0.0.0.0');
官方:
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if enableLookups
for the connector is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%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)
%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
%F - Time taken to commit the response, in millis
%I - Current request thread name (can compare later with stacktraces)