Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have Apache log slow requests?

Tags:

apache

If a page takes over a couple of seconds to process, I'd like Apache to log that URL somewhere. Is this possible? I have a lot of sites, so I was looking for an automatic way to do this as opposed to proprietary code for each site.

like image 208
Matt Avatar asked Oct 07 '10 04:10

Matt


People also ask

Does Apache log post data?

POST data is sent in the request's body, so Apache can't log POST requests by default. You can log POST request details by using dumpio module for Apache, which allows logging of HTTP request body.


1 Answers

Take a look at http://httpd.apache.org/docs/2.2/mod/mod_log_config.html. You can setup a custom log that includes the time it took to serve the request.

For example:

LogFormat "%h %l %u %t \"%r\" %>s %b %D" common-time

Would add the time in microseconds it took to serve the request as the last field of your logfile.

You would add that line to your httpd.conf, then in each virtualhost where you want to use it, add this line:

CustomLog logs/access_log_time common-time

You could also create a new LogFormat that contains only exactly what you want, maybe like this:

LogFormat "\"%r\" %D" measure-time

In your virtualhost, you can have multiple logs, so you could have:

CustomLog logs/access_log common
CustomLog logs/access_log_time measure-time

All that said, there's a huge caveat. This will measure only the time it takes the server to serve the page. It will not include the time it takes to execute any javascript in the browser. If you need to measure javascript execution time, you'll need to use a tool like firebug.

Once you've got the log, you could use something like apachelog to parse the logfile to get only the requests that took longer than whatever threshold you want to use.

I'm not sure if it's possible to log only the long requests and bypss the parsing step. It might be, but i have a feeling it would take a significant amount of work.

like image 152
bradym Avatar answered Sep 20 '22 16:09

bradym