[Catalyst] Using Log4perl -- setting caller()

Bill Moseley moseley at hank.org
Mon Jul 2 16:04:40 GMT 2007


I have a few questions about using Log4perl.  I want to send warnings
to the logger and also include some request info.

I'm using Catalyst::Log::Log4perl, and it uses caller() to set the
logger category.

Now, one thing I was playing with is having warnings sent to log4perl.
There's C::P::LogWarnings but it uses Class:C3 which doesn't agree
with the Session plugins.

So In a plugin to seutp Log4perl I modified the code from
C::P::LogWarnings:

    sub handle_request {
        my $c = shift;
        return $c->NEXT::handle_request( @_ )
            unless $c->config->{log4perl}{log_warnings};

        return do {
            local $SIG{__WARN__} = sub {
                my $warning = shift;
                chomp $warning;
                $c->log->warn( $warning );
            };
            $c->NEXT::handle_request( @_ );
        };
    }

Now, the problem with that is $c->log->warn uses caller() so errors
are reported from that sub, not from where warn was called.  So, need
a way to use (IIRC) caller(1) instead in that case.

Any ideas how to make C::Log::Log4perl be smarter about which caller
to use?


Also, I'd like to include request-specific items in the lot message.

What would be the best way to get $c->req to the logger?  I considered
creating $c->info, $c->warn, and $c->error methods that add the
request data I would like to see to the $message and then forward onto
$c->log->$level.

Or maybe override the log() method to always pass along $c.

How have others solved that problem?



-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list