in reply to Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?

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

  • Comment on Re: Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?

Replies are listed 'Best First'.
Re^2: Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?
by toomas (Initiate) on May 29, 2016 at 22:04 UTC

    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.