splinky has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I'm looking to replace my pathetic home-grown logging package with Log4perl. But I can't find a layout formatter that does what I want - indenting and putting a prefix string before every line in a multi-line string.

For example, with what I have now, I can do the following:

printlog("one\ntwo\n"); indentlog(+1); printlog("three\nfour\n"); indentlog(-1); pringlog("five\nsix\n");
The output would look like this:
05/31/06 17:57:37 | one 05/31/06 17:57:37 | two 05/31/06 17:57:37 | three 05/31/06 17:57:37 | four 05/31/06 17:57:37 | five 05/31/06 17:57:37 | six
Obviously, the PatternLayout class can take care of the prefix, but it doesn't handle embedded newlines. And there's an example of doing indentation in the FAQ, but that's with a custom Appender, and I want that functionality in the Layout class so I can use it with both the File appender and the Screen appender.

Surely I'm not the only one that wants something like this. Worst case, I can modify my printlog subroutine to use Log4perl, but I'm hoping for something better than that.

Any suggestions? Thanks.

Replies are listed 'Best First'.
Re: Indenting, multi-line Log4perl Layout class?
by saintmike (Vicar) on Jun 02, 2006 at 20:47 UTC
    Your logfile output seems to indicate that what you really want is to split up a message that contains N newlines into N+1 separate messages (note the duplicate timestamps). That could be confusing, so I don't think Log4perl would want to support that.

    If you just want to replace newlines by newline+indentation, as you've mentioned, you could use the appender in the FAQ.

    To use this appender in conjunction with other appenders, you can define it as a 'composite' appender, Log::Log4perl::Appender::Buffer shows an example.

    I could also envision a wrapper class around Log4perl that breaks up long messages before handing them over to Log4perl.

    And on the layout level, maybe a custom cspec with some additional magic could do the trick.

    Or, try to convince the guys on the log4perl-devel@ mailing list to add a layout element to the PatternLayout that does line breaking. Seems like a useful feature.