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 | Short Link | Comments (0)

XML

Blog Categories

software (39)
..cocoa (21)
  ..heads up 'tunes (5)
..ruby (6)
..lisp (3)
..perl (4)
..openacs (1)
mac (20)
embedded (2)
..microprocessor (2)
  ..avr (1)
electronics (3)
design (1)
photography (25)
..black and white (6)
..A day in Sydney (18)
..The Daily Shoot (6)
food (2)
Book Review (2)

Notifications

Icon of Envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. Unregistered Visitor: An other Script
  2. Unregistered Visitor: A message in there?
  3. Unregistered Visitor: using Amazon S3
  4. Unregistered Visitor: Thank you ! Thank you ! Thank you !
  5. Unregistered Visitor: Umbrello on leopard
  6. Unregistered Visitor: Script gor generate for library
  7. Unregistered Visitor: Similar but different
  8. Unregistered Visitor: Thanks for fixing my problem!
  9. Unregistered Visitor: Pop up once the category is been defined
  10. Unregistered Visitor: smal amendment