about photos bookshelf portfolio blog home
Begin main content

Custom Apache logging with values from your mod_perl code

I recently had a need to create a custom Apache log which contained data from within the perl code for a page request.

It turns out to be rather simple, using the Apache concept of notes.

Anywhere in your perl code, you need to add a snippet like this:

$r->notes( foo_log_info => $foo_value );

If you are using mod_perl your handler is a good places to do it, mason users could also use their autohandler, but you can really put it anywhere that you have access to the $r Apache::Request object.

In your httpd.conf section, do something like this:

SetEnvIf Request_URI "\.html$" foo_log_this_page
SetEnvIf Request_URI "/$" foo_log_this_page
LogFormat "%h %{foo_log_info}n" foo_log_format
CustomLog foo_log foo_log_format env=foo_log_this_page

See the apache docs for info on what the other fields mean (
http://httpd.apache.org/docs/logs.html ), but the important things are these:

  • The SetEnvIf directives, along with the env= tail to the CustomLogdirective make sure that you are only logging html files (or directory indexes). You may need to change this depending on what files are served by mod_perl in your setup.

  • The %{foo_log_info}n is the note that you set in the perl code appearing in the LogFormat.

  • CustomLog sets up a new log file based on our custom format (log file path is relative to your ServerRoot).

11:12 AM, 03 Feb 2005 by Mark Aufflick Permalink

Add comment