Viner, David wrote on 7/9/2004, 4:03 PM:
> from reading the perldoc page, I noticed that I can modify the value of
> $Log::Log4perl::caller_depth if I use the logger from a wrapper class.
>
> how can I discover at runtime by how much to increment this value?
>
> that is, my wrapper class might be used in a variety of different
> "depths".
> How can I figure out what to add to caller_depth to make it work
> properly?
This reminds me of a post that we had a while ago here, regarding
finding out how to indent log messages, based on the caller level, check
it out:
Pete Siemsen wrote on 12/29/2003, 4:37 PM:
> It seems like "Custom Cspecs" in PatternLayouts is the way to do
> this in log4perl.
If you don't mind the expense of a bunch of caller() calls per logged
message, here's a solution:
use Log::Log4perl qw(get_logger);
####################
sub level {
####################
my $i = 0;
$i++ while caller($i);
return $i-6;
}
my $conf = q(
log4perl.PatternLayout.cspec.Y = sub { " " x level() }
log4perl.category = WARN, Logfile
log4perl.appender.Logfile = Log::Log4perl::Appender::Screen
log4perl.appender.Logfile.layout = PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %Y%m%n
);
Log::Log4perl::init(\$conf);
my $logger = get_logger("");
$logger->error("first");
bar();
$logger->error("first");
sub bar { $logger->error("second"); foo(); }
sub foo { $logger->error("third"); }
__END__
The $i-6 expression might need to be adapted if caller_level is set to
anything but 0. Here's the output:
first
second
third
first
--
-- Mike
Mike Schilli
m...@pe...
|