? ? ? ? 當(dāng)系統(tǒng)卡頓時(shí)候,我們需要分析時(shí)間花費(fèi)在哪個(gè)緩解。項(xiàng)目的后端接口可以記錄一些時(shí)間,此外,在我們的tomcat容器和nginx網(wǎng)關(guān)上也可以記錄一些有關(guān)請(qǐng)求用戶(hù),請(qǐng)求時(shí)間,響應(yīng)時(shí)間的數(shù)據(jù),可以提供更多的信息以便于排查問(wèn)題。
1.tomcat日志
server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %I %t "%r" %s %b %D" />
以下列出了一些基本的日志格式項(xiàng):
%a – 遠(yuǎn)程主機(jī)的IP (Remote IP address)
%A – 本機(jī)IP (Local IP address)
%b – 發(fā)送字節(jié)數(shù),不包含HTTP頭,0字節(jié)則顯示 ‘-’ (Bytes sent, excluding HTTP headers, or ‘-’ if no bytes
were sent)
%B – 發(fā)送字節(jié)數(shù),不包含HTTP頭 (Bytes sent, excluding HTTP headers)
%h – 遠(yuǎn)程主機(jī)名 (Remote host name)
%H – 請(qǐng)求的具體協(xié)議,HTTP/1.0 或 HTTP/1.1 (Request protocol)
%l – 遠(yuǎn)程用戶(hù)名,始終為 ‘-’ (Remote logical username from identd (always returns ‘-’))
%m – 請(qǐng)求方式,GET, POST, PUT (Request method)
%p – 本機(jī)端口 (Local port)
%q – 查詢(xún)串 (Query string (prepended with a ‘?’ if it exists, otherwise
an empty string)
%r – HTTP請(qǐng)求中的第一行 (First line of the request)
%s – HTTP狀態(tài)碼 (HTTP status code of the response)
%S – 用戶(hù)會(huì)話(huà)ID (User session ID)
%t – 訪(fǎng)問(wèn)日期和時(shí)間 (Date and time, in Common Log Format format)
%u – 已經(jīng)驗(yàn)證的遠(yuǎn)程用戶(hù) (Remote user that was authenticated
%U – 請(qǐng)求的URL路徑 (Requested URL path)
%v – 本地服務(wù)器名 (Local server name)
%D – 處理請(qǐng)求所耗費(fèi)的毫秒數(shù) (Time taken to process the request, in millis)
%T – 處理請(qǐng)求所耗費(fèi)的秒數(shù) (Time taken to process the request, in seconds)
你可以用以上的任意組合來(lái)定制你的訪(fǎng)問(wèn)日志格式,也可以用下面兩個(gè)別名common和combined來(lái)指定常用的日志格式:
common – %h %l %u %t "%r" %s %b
combined -
%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
另外你還可以將cookie, 客戶(hù)端請(qǐng)求中帶的HTTP頭(incoming header), 會(huì)話(huà)(session)或是ServletRequest中的數(shù)據(jù)都寫(xiě)到Tomcat的訪(fǎng)問(wèn)日志中,你可以用下面的語(yǔ)法來(lái)引用。
%{xxx}i – 記錄客戶(hù)端請(qǐng)求中帶的HTTP頭xxx(incoming headers)
%{xxx}c – 記錄特定的cookie xxx
%{xxx}r – 記錄ServletRequest中的xxx屬性(attribute)
%{xxx}s – 記錄HttpSession中的xxx屬性(attribute)
2.nginx日志
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"login_user":"$cookie_login_user",'
'"http_host":"$host",'
'"request":"$request",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
request_time(單位為秒)
官網(wǎng)描述:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client。
指的就是從接受用戶(hù)請(qǐng)求的第一個(gè)字節(jié)到發(fā)送完響應(yīng)數(shù)據(jù)的時(shí)間,即$request_time包括接收客戶(hù)端請(qǐng)求數(shù)據(jù)的時(shí)間、后端程序響應(yīng)的時(shí)間、發(fā)送響應(yīng)數(shù)據(jù)給客戶(hù)端的時(shí)間(不包含寫(xiě)日志的時(shí)間)。
upstream_response_time(單位為秒)
官網(wǎng)描述:keeps time spent on receiving the response from the upstream server; the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable.。
是指從Nginx向后端建立連接開(kāi)始到接受完數(shù)據(jù)然后關(guān)閉連接為止的時(shí)間。
從上面的描述可以看出,$request_time肯定比$upstream_response_time值大;尤其是在客戶(hù)端采用POST方式提交較大的數(shù)據(jù),響應(yīng)體比較大的時(shí)候。在客戶(hù)端網(wǎng)絡(luò)條件差的時(shí)候,$request_time還會(huì)被放大。
參考文檔:
Tomcat訪(fǎng)問(wèn)日志淺析_org.apache.catalina.valves.accesslogvalve_yaerfeng的博客-CSDN博客文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-624054.html
Nginx - request_time和upstream_response_time詳解_nginx request_time_zzhongcy的博客-CSDN博客文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-624054.html
到了這里,關(guān)于tomcat和nginx的日志記錄請(qǐng)求時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!