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.


In reply to Log::Log4perl::Layout::PatternLayout: setting combined length for two fields? by toomas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.