X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FLog%2FContextual.pm;h=1d0a1dca966de1903ba032dd5c71811c41d98f46;hb=0c180ced3d15f9851c4ae1cf4ea984c026f67b3f;hp=495781ea275fe652ac9d99b6c9f1d8702da08246;hpb=27141a7ad9039b0ae6b731f3f11e56ce11b8fbab;p=p5sagit%2FLog-Contextual.git diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index 495781e..1d0a1dc 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -3,7 +3,7 @@ package Log::Contextual; use strict; use warnings; -our $VERSION = '0.004100'; +our $VERSION = '0.004202'; my @levels = qw(debug trace warn info error fatal); @@ -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 { @@ -254,7 +254,7 @@ C you will be able to swap underlying loggers later. =item * Powerful -C chooses which logger to use based on L<< user defined Cs|/LOGGER CODEREF>>. +C chooses which logger to use based on L<< user defined Cs|/LOGGER CODEREF >>. Normally you don't need to know this, but you can take advantage of it when you need to later @@ -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}; }