Skip Menu |

This queue is for tickets about the Log-Log4perl CPAN distribution.

Report information
The Basics
Id: 45559
Status: resolved
Priority: 0/
Queue: Log-Log4perl

People
Owner: Nobody in particular
Requestors: TIMB [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Handling of newlines in messages is limited (/%m%n/ should not be a special case)
In Log/Log4perl/Layout/PatternLayout.pm define() it says: # If the message contains a %m followed by a newline, # make a note of that so that we can cut a superfluous # \n off the message later on if($format =~ /%m%n/) { $self->{message_chompable} = 1; } else { $self->{message_chompable} = 0; } That behaviour is not documented. I'd like to suggest changing the pattern to simply /%n/ so chomp'ing is the default behaviour unless the format doesn't contain %n. (Alternatively, change the pattern to /%m.*%n/ so that other placeholders can be used after the %m but before the newline: "... %m (%M %F)%n". And/or add a way to express "%m but chomp'd" as a placeholder, perhaps %m{chomp}. But both those changes add complexity compared with simply using /%n/ as the pattern.)
Show quoted text
> That behaviour is not documented.
Added: http://github.com/mschilli/log4perl/commit/8b17b536a67229c0489fd496ca79736a3a4f8a3b Show quoted text
> I'd like to suggest changing the pattern to simply /%n/ so chomp'ing > is the default behaviour > unless the format doesn't contain %n.
I'm curious, what's the use case that triggers this problem? Is there a case where you would want "%m something else %n" to chomp %m?
CC: TIMB [...] cpan.org
Subject: Re: [rt.cpan.org #45559] Handling of newlines in messages is limited (/%m%n/ should not be a special case)
Date: Wed, 29 Apr 2009 17:36:37 +0100
To: Michael_Schilli via RT <bug-Log-Log4perl [...] rt.cpan.org>
From: Tim Bunce <Tim.Bunce [...] pobox.com>
On Wed, Apr 29, 2009 at 11:57:07AM -0400, Michael_Schilli via RT wrote: Show quoted text
>
> > I'd like to suggest changing the pattern to simply /%n/ so chomp'ing > > is the default behaviour > > unless the format doesn't contain %n.
> > I'm curious, what's the use case that triggers this problem? Is there a > case where you would want "%m something else %n" to chomp %m?
Yes, where the log message ends with a newline but you want extra information, like %M, %F etc etc, to be added after the message instead of before. Tim.
Show quoted text
> Yes, where the log message ends with a newline but you want extra > information, like %M, %F etc etc, to be added after the > message instead of before.
Problem is we need to balance different needs for different users. This feature came about because noobs were using use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); DEBUG "waah!\n"; and were wondering why they were getting two newlines. (What goes on behind the scenes is that "%m%n" is used as a layout). In hindsight, that probably wasn't the smartest decision, but I guess people got used to it, and we can't take it away easily. Here's my take: using a newline in a log message isn't recommended if you don't want it to be taken literally. Chomping the message in "%m%n" while leaving it unchomped in other cases caters the noobs, but leaves open the possibility that an experienced user actually meant to add a newline. Wouldn't it be easier to get rid of the newlines in the log messages instead?
CC: TIMB [...] cpan.org
Subject: Re: [rt.cpan.org #45559] Handling of newlines in messages is limited (/%m%n/ should not be a special case)
Date: Thu, 30 Apr 2009 10:57:53 +0100
To: Michael_Schilli via RT <bug-Log-Log4perl [...] rt.cpan.org>
From: Tim Bunce <Tim.Bunce [...] pobox.com>
On Wed, Apr 29, 2009 at 04:03:12PM -0400, Michael_Schilli via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=45559 > >
> > Yes, where the log message ends with a newline but you want extra > > information, like %M, %F etc etc, to be added after the > > message instead of before.
> > Problem is we need to balance different needs for different users. This > feature came about because noobs were using > > use Log::Log4perl qw(:easy); > Log::Log4perl->easy_init($DEBUG); > DEBUG "waah!\n"; > > and were wondering why they were getting two newlines. (What goes on > behind the scenes is that "%m%n" is used as a layout). In hindsight, > that probably wasn't the smartest decision, but I guess people got used > to it, and we can't take it away easily. > > Here's my take: using a newline in a log message isn't recommended if > you don't want it to be taken literally.
(I see a vague parallel with the terminator vs separator issue with commas and semicolons.) Show quoted text
> Chomping the message in "%m%n" while leaving it unchomped in other cases > caters the noobs, but leaves open the possibility that an experienced > user actually meant to add a newline.
I think experienced users are also likely to want to add information after the message. Show quoted text
> Wouldn't it be easier to get rid of the newlines in the log messages > instead?
Umm. I believe log4j assumes log messages don't end with a newline. On the other hand, Log::Dispatch takes the view that they should http://search.cpan.org/~drolsky/Log-Dispatch/lib/Log/Dispatch.pm#Why_doesn't_Log::Dispatch_add_a_newline_to_the_message? [Curiously, the examples on the main Log::Dispatch page don't contain a newline, but the example on the Log::Dispatch::File does! That goes against the principle of keeping the act of logging by the application separate from where the messages end up.] I couldn't find any discussion of this issue in the docs. Also, the examples in the docs are inconsistent in their use of newlines. Some do, some don't. That should be fixed. So some users, especially on large teams, might find themselves with some code using trailing newlines and some not. Two suggestions: 1. Add a config option to control chomping: true, false, or undef, where undef is the current (undocumented) /%m%n/ mechanism. 2. Add a placeholder that means "message but chomped", perhaps %m{chomp}. Neither would change existing behaviour by default, and both provide a solution to the issue. Tim.
Show quoted text
> Two suggestions: > 1. Add a config option to control chomping: true, false, or undef, > where > undef is the current (undocumented) /%m%n/ mechanism. > 2. Add a placeholder that means "message but chomped", perhaps > %m{chomp}.
Excellent, I like it! Committed to the repo: http://github.com/mschilli/log4perl/commit/e9256a8425e67bca36631982cd05060cbed3b398
CC: TIMB [...] cpan.org
Subject: Re: [rt.cpan.org #45559] Handling of newlines in messages is limited (/%m%n/ should not be a special case)
Date: Sat, 2 May 2009 15:39:31 +0100
To: Michael_Schilli via RT <bug-Log-Log4perl [...] rt.cpan.org>
From: Tim Bunce <Tim.Bunce [...] pobox.com>
On Fri, May 01, 2009 at 10:53:45PM -0400, Michael_Schilli via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=45559 > >
> > Two suggestions: > > 1. Add a config option to control chomping: true, false, or undef, > > where > > undef is the current (undocumented) /%m%n/ mechanism. > > 2. Add a placeholder that means "message but chomped", perhaps > > %m{chomp}.
> > Excellent, I like it! Committed to the repo: > > http://github.com/mschilli/log4perl/commit/e9256a8425e67bca36631982cd05060cbed3b398
Great. Thanks. Tim.
Resolved, will be released with 1.22.