21.9.3. Discussion
Apache calls logging handlers after sending the response to the
client. You have full access to the request and response parameters,
such as client IP address, headers, status, and even content. Access
this information through method calls on the request object.
You'll probably want to escape values before writing them to a text
file because spaces, newlines, and quotes could spoil the formatting
of the files. Two useful functions are:
# return string with newlines and double quotes escaped
sub escape {
my $a = shift;
$a =~ s/([\n\"])/sprintf("%%%02x", ord($1))/ge;
return $a;
}
# return string with newlines, spaces, and double quotes escaped
sub escape_plus {
my $a = shift;
$a =~ s/([\n \"])/sprintf("%%%02x", ord($1))/ge;
return $a;
}
Apache::DBILogger is a more general interface, logging each hit as a
new entry in a table. The table has columns for data such as which
virtual host delivered the data, the client's IP address, the user
agent (browser), the date, the number of bytes transferred, and so
on. Using this table and suitable indexes and queries, you can answer
almost any question about traffic on your web site.
Because the logging handler runs before Apache has closed the
connection to the client, don't use this phase if you have a slow
logging operation. Instead, install the handler with
PerlCleanupHandler so that it runs after the connection is closed.