Example code demonstrating how to log messages under mod_perl (version 1). The code below uses a custom remark subroutine that operates the same if called at server startup time or during a request. Alternatives include modules such as Log::Dispatch.
use Apache::Log ();
unless ($loaded) {
remark( 'warn', 'could not load file', { file => $file, errno => $! } );
}
sub handler {
unless ($something) {
remark( 'error', 'no response from database', { errno => $dbi->errstr } );
return DECLINED;
}
}
sub remark {
my $priority = shift;
my $message = shift;
my $attributes = shift;
chomp $message;
my $attr_str;
if ($attributes) {
$attr_str = join ', ',
map { $attributes->{$_} ||= ''; "$_=$attributes->{$_}" }
sort keys %$attributes;
}
my $r = Apache->request || Apache->server;
$r->log->$priority($message . ( $attr_str ? ": $attr_str" : '' ) );
}