|
From: Anno S. <ann...@ma...> - 2008-10-14 23:50:55
|
On 14.10.2008, at 20:22, todd.e.rinaldo wrote:
> Hi,
>
> I often have a helper routine in my code that logs an event and then
> does stuff. In some cases, it would be helpful if the line number that
> is logged was one level up from where I call debug.
>
> My question: does functionality exist in l4p similar to Test::More
> that would allow me to log events from higher in the stack than where
> I called debug?
>
> Test::More method:
> local $Test::Builder::Level++;
I don't think you can modify the behavior of %L, and hence SimpleLayout
in this way. If you're ready to define your own layout, the feature can
be added via a custom placeholder (%A below). See the appended code for
a way to do it.
Anno
----------------------------------------------------------------------
use Log::Log4perl qw(get_logger);
Log::Log4perl::init(\ *DATA);
log_this("here line " . __LINE__); # show real line number in log
message
log_this("here line " . __LINE__); # again, one line down
exit;
sub log_this {
get_logger()->info(shift); # %L will be this line number, %A won't
}
# helper sub for custom placeholder %A
sub line_one_above {
my $i = 1; # skip bottom level
(main)
++ $i while caller($i) =~ /^Log::Log4perl/; # climb to user spoace
++ $i; # one more (!)
(caller($i))[2]; # return line number
}
__DATA__
log4perl.rootLogger=INFO, screen_root
log4perl.appender.screen_root=Log::Log4perl::Appender::Screen
log4perl.appender.screen_root.layout=PatternLayout
log4perl.appender.screen_root.layout.ConversionPattern=%m at line %A
(L=%L)%n
log4perl.PatternLayout.cspec.A = sub { line_one_above() }
|