Tuesday, December 6, 2011

Passing PHP data to Apache access logs

Recently, I worked on a project to collect performance data from an application at work and pass that data to Apache access logs as part of a performance improvement project. As it turns out, PHP has a built-in function called apache_note() that your can use for this purpose.

For example, if you wish to pass memory usage information to Apache access logs, add the following code to at the end of your front controller file:

if (php_sapi_name() != 'cli') {
    apache_note('PHPMemoryUsage', memory_get_peak_usage(true));
}

Then, update your Apache LogFormat directive to include this new piece of information:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{PHPMemoryUsage}n" combined

No comments:

Post a Comment