Skip Menu |

This queue is for tickets about the Log-Handler CPAN distribution.

Report information
The Basics
Id: 75596
Status: patched
Priority: 0/
Queue: Log-Handler

People
Owner: Nobody in particular
Requestors:
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.71
Fixed in: (no value)



Subject: %s placeholder
Currently, with the %s placeholder in message_layout, the subroutine name is always reported as Log::Handler::Levels::__ANON__. Should _get_c_sub in Handler/Pattern.pm be replaced by something like: sub _get_c_sub { (caller(3+$Log::Handler::CALLER_LEVEL))[3]//"" } ? Thanks, David
On Tue Mar 06 20:51:07 2012, http://dr1024.myopenid.com/ wrote: Show quoted text
> Currently, with the %s placeholder in message_layout, the subroutine > name is always reported as Log::Handler::Levels::__ANON__. > > Should _get_c_sub in Handler/Pattern.pm be replaced by something like: > > sub _get_c_sub { (caller(3+$Log::Handler::CALLER_LEVEL))[3]//"" }
No. Log::Handler returns what caller returns. --- use strict; use warnings; use Data::Dumper; sub foo { print Dumper([ caller(0) ]); } my $bar = sub { print Dumper([ caller(0) ]); }; &foo(); &$bar(); --- $VAR1 = [ 'main', 'test.pl', 14, 'main::foo', 1, undef, undef, undef, 1794, 'UUUUUUUUUUU', undef ]; $VAR1 = [ 'main', 'test.pl', 15, 'main::__ANON__', 1, undef, undef, undef, 1794, 'UUUUUUUUUUU', undef ];
But -------------------- test-bug.pl use strict; use warnings; use Log::Handler; my $log = Log::Handler->new(); $log->add(screen => { maxlevel => "debug", message_layout => '%f:%l %s %m', }); sub foo { $log->debug("hello from foo"); } foo(); -------------------- outputs: test-bug.pl:14 Log::Handler::Levels::__ANON__ hello from foo Increasing the caller level by 1 inside _get_c_sub makes the %s output more useful. David On Tue Mar 06 21:00:23 2012, BLOONIX wrote: Show quoted text
> On Tue Mar 06 20:51:07 2012, http://dr1024.myopenid.com/ wrote:
> > Currently, with the %s placeholder in message_layout, the subroutine > > name is always reported as Log::Handler::Levels::__ANON__. > > > > Should _get_c_sub in Handler/Pattern.pm be replaced by something like: > > > > sub _get_c_sub { (caller(3+$Log::Handler::CALLER_LEVEL))[3]//"" }
> > No. Log::Handler returns what caller returns.
Show quoted text
> outputs: > > test-bug.pl:14 Log::Handler::Levels::__ANON__ hello from foo > > Increasing the caller level by 1 inside _get_c_sub makes the %s output > more useful.
Hmm, you could be right. Let me do some tests.
Unfortunately that is not possible. Let me explain it: --- use Log::Handler; $Log::Handler::CALLER_LEVEL = 1; my $log = Log::Handler->new(); $log->add(screen => { message_layout => '%m %s', debug_trace => 1 }); $log->warn("foo"); --- Output: Use of uninitialized value in concatenation (.) or string at (eval 8) line 1. foo CALL(2): package(main) filename(test.pl) line(8) subroutine (Log::Handler::Levels::__ANON__) hasargs(1) CALL(1): package(Log::Handler::Levels) filename(/usr/local/share/ perl/5.10.1/Log/Handler/Levels.pm) line(212) subroutine (Log::Handler::Output::log) hasargs(1) wantarray(0) CALL(0): package(Log::Handler::Output) filename(/usr/local/share/ perl/5.10.1/Log/Handler/Output.pm) line(96) subroutine (Log::Handler::Output::_add_trace) hasargs(1) You can see here that just "foo" is printed, but not the subroutine or line number, because caller level 3 does not exists. Next example: --- use Log::Handler; $Log::Handler::CALLER_LEVEL = 1; my $log = Log::Handler->new(); $log->add(screen => { message_layout => '%m %s line %l' }); &foo; sub foo { $log->warn("foo"); $log->warn("bar"); $log->warn("baz"); } --- Output: foo main::foo line 8 bar main::foo line 8 baz main::foo line 8 I don't think that this way of debugging is the right way. We just see every time the same line number. If the package or subroutine is very large then I want to see on which line the logger was called and not which subroutine above called the logger. If you want that, then please set CALLER_LEVEL to 1. $Log::Handler::CALLER_LEVEL = 1; Cheers, Jonny
I forgot... You can use your own placeholder or overwrite %s if you really want that. my $log = Log::Handler->new(); $log->set_pattern("%s", "subroutine", sub { (caller (3+$Log::Handler::CALLER_LEVEL))[3] });
I think both your examples above work ok with the patch I suggested. As you say, calling $log->warn outside any subroutine (as in your first example) would cause an issue with %s, hence catch that case with //"" (as in the patch). Only _get_c_sub needs any change (globally raising CALLER_LEVEL is not the solution). Thanks, David On Wed Mar 07 11:32:43 2012, BLOONIX wrote: Show quoted text
> I forgot... > > You can use your own placeholder or overwrite %s if you really want that. > > my $log = Log::Handler->new(); > $log->set_pattern("%s", "subroutine", sub { (caller > (3+$Log::Handler::CALLER_LEVEL))[3] });
Sorry David, but I will not patch _get_c_sub. If you are unlucky with the placeholder %s then please use set_pattern. Cheers Jonny
see 0.73