Printing Response time on apache and nginx logs
Everytime when a developer comes to you and complains this website is SLOW! and you are like the servers are strong enough to handle 5 of your dev team and couple of QA! check your code ! but then this doesn’t work in real life because you need to provide them stats. Do provide stats there are unless parameters which needs to be monitored but thats an endless topic. So to start with we will first stick to basics. Adding a response time on your web application log.
Adding response time in Nginx
Before we plan to add lets see how the current access log look
172.31.0.97 — — [18/Dec/2021:03:42:09 +0000] "GET / HTTP/1.1" 404 160 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "42.93.25.45"
Open your nginx.conf file and search for log_format block and replace with
log_format main '$remote_addr|$time_local]|$request|$request_time|$upstream_respons _time|' '$status|$body_bytes_sent|$http_referer|'
'$http_user_agent';
Save this file and restart your nginx and then make a request and lets print how it looks on your log file now
172.31.0.97|18/Dec/2021:04:35:50 +0000]|GET / HTTP/1.1|0.125|0.124|404|160|-|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
the two values in bold are request_time and upstream_response_time
Adding response time in httpd/apache2
Likewise how we did in nginx, Lets print the current log file output from apache’s access log
172.31.0.97 - - [18/Dec/2021:04:19:24 +0000] "GET / HTTP/1.1" 403 3630 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
Open httpd.conf or apache2.conf and update this line on code
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Save this file and restart your apache2/httpd and then make a request and lets print how it looks on your log file now
49.37.221.178 - - [18/Dec/2021:04:22:30 +0000] "GET / HTTP/1.1" 403 3630 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" **0/449**
The bold values are response time in milliseconds.
If this article has helped you, connect with me on linkedin