toomas has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
Recently I discovered Log::Log4perl. This is such a great piece of work that after a week of using it I'm almost wondering how could I live without it before. But there are things about it I have not been able to figure out by studying the documentation.
Here is my problem:
I want to indent log messages by stack depth (for tracing program execution), and although Log::Log4perl itself does not directly support this, I have managed (with help from the Internet, including Perl Monks) to have it my way — almost. I added the following lines to log4perl.conf:
log4perl.PatternLayout.cspec.S = sub { return ' ' x level_for_l4p() +; } log4perl.appender.std.layout.ConversionPattern = %-27F %3L %S%m%n
and defined level_for_l4p() in my main program:
my $zerolevel = 8; sub level_for_l4p { my $level = 0; 1 while caller( $level++ ); return ( $level - $zerolevel ); } Log::Log4perl::init( 'log4perl.conf' );
(Appropriate numbers for $zerolevel and %F are easily found by trial and error.)
Logs produced by this setup look basically like this:
/My/Project/One/File.pm 12 sub0: calling sub1 /My/Project/Another/File.pm 96 sub1: entering /My/Project/Another/File.pm 105 sub1: calling sub2 /My/Project/Elsewhere.pm 72 sub2: entering /My/Project/Elsewhere.pm 84 sub2: leaving /My/Project/Another/File.pm 108 sub1: continuing after sub2 /My/Project/Another/File.pm 115 sub1: leaving /My/Project/One/File.pm 16 sub0: continuing after sub1 # etc.
In principle, this is exactly what I want, but I don't like the appearance of the output: IMHO it fails miserably to please the eye.
I'd much prefer being able to have the log output look like this:
/My/Project/One/File.pm:12: sub0: calling sub1 /My/Project/Another/File.pm:96: sub1: entering /My/Project/Another/File.pm:105: sub1: calling sub2 /My/Project/Elsewhere.pm:72: sub2: entering /My/Project/Elsewhere.pm:84: sub2: leaving /My/Project/Another/File.pm:108: sub1: continuing after sub2 /My/Project/Another/File.pm:115: sub1: leaving /My/Project/One/File.pm:16: sub0: continuing after sub1
which would also have the additional benefit of working right out of the box with Emacs' default compilation-error-regexp-alist. To achieve this, it should be possible to say something like %-32{%F:%L:}, i.e. to tell Log::Log4perl to set the total length for %F:%L: as a unit. But I have not been able to find a way to express my intention.
Is it possible to do what I want? In case I'm missing something obvious, can anybody point me to an existing and/or simple solution?
I am using Log::Log4perl 1.47 and Perl 5.20.0.
Cheers,
T.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?
by clueless newbie (Curate) on May 28, 2016 at 12:07 UTC | |
|
Re: Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?
by Anonymous Monk on May 28, 2016 at 07:21 UTC | |
by toomas (Initiate) on May 29, 2016 at 22:04 UTC |