How to enable response time logging in Jetty 7

2019-02-13 01:36发布

问题:

Apache and Tomcat both make it easy to log response times out to the server access log (with the %D pattern), but I've been unable to find any equivalent support in Jetty, either with the default NCSARequestLog or using logback-access (my preferred logging setup).

Is there a way of getting Jetty to log these times? I could see it being possible to use a custom handler of some kind to gather this information and make it available to the logging handler, but am hopeful that something like the StatisticsHandler could do it for me as it's clearly already tracking this information to generate its aggregate stats.

回答1:

Call setLogLatency(true) on NCSARequestLog



回答2:

A good detailed explanation on how to enable response time logging through configuration is here: Monitoring latency in Jetty request log

Summary:

Open [JETTY HOME]/etc/jetty.xml.

Locate <Ref id=”RequestLog”> section.

Add Set name=”logLatency”>true to setter list.

Save it and restart Jetty.



回答3:

Using logback access 1.1.0 or later, you can use the %elapsedTime conversion word to get the response time in ms. From https://logback.qos.ch/manual/layouts.html :

D / elapsedTime      The time taken to serve the request, in milliseconds.
T / elapsedSeconds   The time taken to serve the request, in seconds.

For example, using:

<pattern>%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}" %elapsedTime{}ms</pattern>

Will output:

0:0:0:0:0:0:0:1 - - [03/Jun/2018:08:07:12 +1200] "GET /actuator/health HTTP/1.1" 200 15 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" 3ms


标签: Jetty logback