Tomcat8 日志配置

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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,591評(píng)論 19 139
  • Refer to: www.threemeal.com/blog/12/ 中間件 中間件是一個(gè)鉤子框架,它們可以介...
    蘭山小亭閱讀 16,788評(píng)論 9 164
  • 第一章 Nginx簡(jiǎn)介 Nginx是什么 沒有聽過(guò)Nginx?那么一定聽過(guò)它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,023評(píng)論 24 1,002
  • 村里人的錢好騙,是的,電信詐騙、傳銷、非法集資、惡性民間借貸還有很多不是常見錢財(cái)詐騙里面的事兒,都是重點(diǎn)發(fā)生在老人...
    艷云閱讀 274評(píng)論 0 2
  • 溪雨-2016-8-16 2016年8月3號(hào)晚上,我們手牽手散了一會(huì)步。然后你停下來(lái),平靜的和我說(shuō)“你是不是要和我...
    田間阡陌閱讀 416評(píng)論 0 0

友情鏈接更多精彩內(nèi)容