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
    Hi,

    If I were attempting to do something along these lines I'd have a look at Flavio Poletti's Log::Log4perl::Tiny. Adding a new "format" shouldn't be difficult - look for the section beginning with the comment "# %format_for idea from Log::Tiny by J. M. Adler".

Re: Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?
by Anonymous Monk on May 28, 2016 at 07:21 UTC

    I thought it could be done with cspec, but the callback doesn't get %info, so the only solution is to create your own version of Log::Log4perl::Layout::PatternLayout

      Hi,

      This was bad news indeed, because I don't really like the idea of redefining (parts of) Log::Log4perl (or Log::Log4perl::Tiny, as per clueless newbie's suggestion). But your reply made me take a closer look at the information available to cspec callbacks, and I was able to snatch good news from the jaws of the bad news: although the callbacks don't get %info, they do get $caller_level, using which I can get at filename and line number on my own (duplicating Log::Log4perl's work a bit). After some experimenting, I came up with a complete solution.

      In log4perl.conf, I say:

      log4perl.PatternLayout.cspec.S = sub { ' ' x level_for_l4p(); } log4perl.PatternLayout.cspec.A = sub { msgpfx_for_l4p( @_ ); } log4perl.appender.std.layout.ConversionPattern = %-32A%S%m%n

      And in the main program:

      my $zerolevel = 8; sub level_for_l4p { my $level = 0; 1 while caller( $level++ ); return $level - $zerolevel; } sub msgpfx_for_l4p { my ( $l, $m, $c, $p, $caller_level ) = @_; my ( $unused, $filename, $line, @unused ) = caller( $caller_level + 1 ); return "$filename:$line:"; } Log::Log4perl::init('log4perl.conf');

      This setup produces the exact output I wished for and described near the end of my initial message, making me happy again.

      Thanks and regards,
      T.