logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Log Multi-Line Messages
Date Wed, 30 Apr 2008 17:18:06 GMT

On Apr 30, 2008, at 10:58 AM, Britton, David wrote:

> Robert Pepersack wrote:
>> Note that the second line isn't prefixed with the text
>> created by my subclass of PatternLayout.
>>
>> How can I get all the output to be prefixed with the layout
>> that I specify?  For example:
>>
>> 2008-04-30 08:59:01 [MyThread] DEBUG MyClass.myMethod(): 220
>> Connected to server. 2008-04-30 08:59:01 [MyThread] DEBUG
>> MyClass.myMethod(): 220 Connection will close if idle for more than 5
>> minutes.
>>
>
> Hi Robert --
>
> You would need to split the line on the line separator, and call a  
> log method for each line.
>

If you write your message like that, a casual reader of the log would  
assume that there were multiple log calls.  I think it would be  
cleaner, just to indent the second and subsequent lines so that you get:

2008-04-30 08:59:01 [MyThread] DEBUG MyClass.myMethod(): 220 Connected  
to server.
                                                                                         
                           220 
  Connection will close if idle for more than 5 minutes.


To get that, have your custom layout (which I'm guessing either  
extends or encapsulates PatternLayout) call PatternLayout.format() to  
get the full output.  Then subtract the length of the  
event.getRenderedMessage() from the length of the formatted message to  
get the length of the prolog.  Then split along line boundaries  
prepending the appropriate number of blanks.




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message