From: Matt S Trout Date: Tue, 16 Oct 2012 13:25:01 +0000 (+0000) Subject: move handle_log_request logic into router X-Git-Tag: v0.005000_01~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8527bf52e552c06d11a3bfa1f4584bd18ff0e592;p=p5sagit%2FLog-Contextual.git move handle_log_request logic into router --- diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index df533e9..0296af3 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -56,38 +56,48 @@ sub before_import { if ($spec->config->{log}) { $spec->add_export("&log_$level", sub (&@) { my ($code, @args) = @_; - my @loggers = $router->get_loggers(scalar(caller), $level); - foreach my $logger (@loggers) { - $logger->$level($code->(@args)); - } + $router->handle_log_request({ + package => scalar(caller), + caller_level => 1, + level => $level, + }, $code, @args); return @args; }); $spec->add_export("&logS_$level", sub (&@) { - my $code = shift; - my @loggers = $router->get_loggers(scalar(caller), $level); - foreach my $logger (@loggers) { - $logger->$level($code->(@_)); - } - return shift; + my ($code, @args) = @_; + $router->handle_log_request({ + package => scalar(caller), + caller_level => 1, + level => $level, + }, $code, @args); + return $args[0]; }); } if ($spec->config->{dlog}) { $spec->add_export("&Dlog_$level", sub (&@) { my ($code, @args) = @_; - my $dumped = (@args?Data::Dumper::Concise::Dumper @args:'()'); - my @loggers = $router->get_loggers(scalar(caller), $level); - foreach my $logger (@loggers) { - $logger->$level(do { local $_ = $dumped; $code->(@args); }); - } + my $wrapped = sub { + local $_ = (@_?Data::Dumper::Concise::Dumper @_:'()'); + &$code; + }; + $router->handle_log_request({ + package => scalar(caller), + caller_level => 1, + level => $level, + }, $wrapped, @args); return @args; }); $spec->add_export("&DlogS_$level", sub (&$) { my ($code, $ref) = @_; - my $dumped = Data::Dumper::Concise::Dumper $ref; - my @loggers = $router->get_loggers(scalar(caller), $level); - foreach my $logger (@loggers) { - $logger->$level(do { local $_ = $dumped; $code->($ref); }); - } + my $wrapped = sub { + local $_ = Data::Dumper::Concise::Dumper($_[0]); + &$code; + }; + $router->handle_log_request({ + package => scalar(caller), + caller_level => 1, + level => $level, + }, $wrapped, $ref); return $ref; }); } diff --git a/lib/Log/Contextual/Role/Router.pm b/lib/Log/Contextual/Role/Router.pm index e127ea6..b6cd31c 100644 --- a/lib/Log/Contextual/Role/Router.pm +++ b/lib/Log/Contextual/Role/Router.pm @@ -4,6 +4,6 @@ use Moo::Role; requires 'before_import'; requires 'after_import'; -requires 'get_loggers'; +requires 'handle_log_request'; 1; diff --git a/lib/Log/Contextual/Router.pm b/lib/Log/Contextual/Router.pm index cbab60e..09235ac 100644 --- a/lib/Log/Contextual/Router.pm +++ b/lib/Log/Contextual/Router.pm @@ -5,6 +5,12 @@ use Scalar::Util 'blessed'; with 'Log::Contextual::Role::Router'; +eval { + require Log::Log4perl; + die if $Log::Log4perl::VERSION < 1.29; + Log::Log4perl->wrapper_register(__PACKAGE__) +}; + sub before_import { } sub after_import { @@ -70,7 +76,10 @@ sub _set_package_logger_for { } sub get_loggers { - my ($self, $package, $level) = @_; + my ($self, $info) = @_; + + my $package = $info->{package}; + my $logger = ( $_[0]->{Package_Logger}->{$package} || $_[0]->{Get_Logger} || @@ -78,11 +87,27 @@ sub get_loggers { die q( no logger set! you can't try to log something without a logger! ) ); - $logger = $logger->($package, { caller_level => 2 }); + my %info = %$info; - return $logger if $logger->${\"is_$level"}; - return (); + $info{caller_level}++; + + $logger = $logger->($package, \%info); + + return $logger if $logger->${\"is_${\$info->{level}}"}; + return (); +} + +sub handle_log_request { + my ($self, $info, $generator, @args) = @_; + + my %info = %$info; + + $info{caller_level}++; + + foreach my $logger ($self->get_loggers(\%info)) { + $logger->${\$info->{level}}($generator->(@args)); + } } -1; +1;