From: Arthur Axel 'fREW' Schmidt Date: Fri, 3 Aug 2012 23:20:19 +0000 (-0500) Subject: fix caller_level passed to coderef, document both uses X-Git-Tag: v0.004202~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=37a8266ad23dae661f325167ad45ff59380c1c5f;p=p5sagit%2FLog-Contextual.git fix caller_level passed to coderef, document both uses --- diff --git a/Changes b/Changes index 01d0718..245d025 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ ChangeLog for Log-Contextual + - correct the caller_level passed into coderef, and document "both" uses of + caller_level + 0.004201 2012-07-21 - The smallest pod fix ever diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index e079a77..fa378de 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -122,7 +122,7 @@ sub _get_logger($) { $Get_Logger || $Default_Logger{$package} || die q( no logger set! you can't try to log something without a logger! ) - )->($package, { caller_level => 3 }); + )->($package, { caller_level => 2 }); } sub set_logger { @@ -547,11 +547,30 @@ The logger coderef is passed the package of the caller the caller level the coderef needs to use if it wants more caller information. The latter is in a hashref to allow for more options in the future. +Here is a basic example of a logger that exploits C to reproduce the +output of C with a logger: + + my @caller_info; + my $var_log = Log::Contextual::SimpleLogger->new({ + levels => [qw(trace debug info warn error fatal)], + coderef => sub { chomp($_[0]); warn "$_[0] at $caller_info[1] line $caller_info[2].\n" } + }); + my $warn_faker = sub { + my ($package, $args) = @_; + @caller_info = caller($args->{caller_level}); + $var_log + }; + set_logger($warn_faker); + log_debug { 'test' }; + The following is an example that uses the information passed to the logger coderef. It sets the global logger to C<$l3>, the logger for the C package to C<$l1>, except the C method in C which uses the C<$l2> logger and lastly the logger for the C package to C<$l2>. +Note that it increases the caller level as it dispatches based on where +the caller of the log function, not the log function itself. + my $complex_dispatcher = do { my $l1 = ...; @@ -573,7 +592,7 @@ logger and lastly the logger for the C package to C<$l2>. my $logger = $registry{'-logger'}; if (my $r = $registry{$package}) { $logger = $r->{'-logger'} if $r->{'-logger'}; - my (undef, undef, undef, $sub) = caller($info->{caller_level}); + my (undef, undef, undef, $sub) = caller($info->{caller_level} + 1); $sub =~ s/^\Q$package\E:://g; $logger = $r->{$sub} if $r->{$sub}; } diff --git a/t/caller.t b/t/caller.t new file mode 100644 index 0000000..3f78612 --- /dev/null +++ b/t/caller.t @@ -0,0 +1,20 @@ +use strict; +use warnings; + +use Log::Contextual::SimpleLogger; +use Test::More qw(no_plan); +use Log::Contextual qw(:log set_logger); +my $var; +my @caller_info; +my $var_log = Log::Contextual::SimpleLogger->new({ + levels => [qw(trace debug info warn error fatal)], + coderef => sub { chomp($_[0]); $var = "$_[0] at $caller_info[1] line $caller_info[2].\n" } +}); +my $warn_faker = sub { + my ($package, $args) = @_; + @caller_info = caller($args->{caller_level}); + $var_log +}; +set_logger($warn_faker); +log_debug { 'test' }; +is($var, "[debug] test at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'fake warn'); diff --git a/t/eg.t b/t/eg.t index 81156b0..0d7843c 100644 --- a/t/eg.t +++ b/t/eg.t @@ -38,7 +38,7 @@ my $complex_dispatcher = do { my $logger = $registry{'-logger'}; if (my $r = $registry{$package}) { $logger = $r->{'-logger'} if $r->{'-logger'}; - my (undef, undef, undef, $sub) = caller($info->{caller_level}); + my (undef, undef, undef, $sub) = caller($info->{caller_level} + 1); $sub =~ s/^\Q$package\E:://g; $logger = $r->{$sub} if $r->{$sub}; }