I already have a custom layout that contains a PatternLayout inside that
does the formatting. But now I have modified the format method as you told
me so it will do the line breakup instead of writting a custom logger.
Here's the code.
/* (non-Javadoc)
* @see org.apache.log4j.Layout#format(org.apache.log4j.spi.LoggingEvent
)
*/
public String format(LoggingEvent event)
{
String message = (String)event.getMessage();
String ret = "";
if (message.length() == 0)
return layout.format(event);
StringTokenizer tokenizer = new StringTokenizer(message, "\n\r");
while(tokenizer.hasMoreElements())
{
LoggingEvent e = new LoggingEvent
(
event.fqnOfCategoryClass,
Logger.getLogger(event.getLoggerName()),
event.timeStamp,
event.getLevel(),
tokenizer.nextToken(),
null
);
ret += layout.format(e);
}
if (event.getThrowableInformation() != null)
{
Throwable throwable = event.getThrowableInformation
().getThrowable();
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
throwable.printStackTrace(ps);
ret += format(new LoggingEvent
(
event.fqnOfCategoryClass,
Logger.getLogger(event.getLoggerName()),
event.timeStamp,
event.getLevel(),
os.toString(),
null
));
}
return ret;
}
/* (non-Javadoc)
* @see org.apache.log4j.Layout#ignoresThrowable()
*/
public boolean ignoresThrowable()
{
return false;
}
This code works fine so far, it performs the line breakup and formats each
line using my pattern layout, both for normal text as for the stacktrace of
throwables. I have doubts thou on whether this is the correct way to do it.
2006/7/18, David Tonhofer <[EMAIL PROTECTED]>:
Roberto Giménez wrote:
> I have a logger that outputs lines which have a prefix. I have a problem
> logging lines that contain newline characters, and the problem is that
> only
> the first line of text contains the prefix, the other ones are logged
> without the prefix. The solution I think is the best is to extend the
> class
> Logger and override the info(...) methods so when Strings are passed
they
> are broken down in lines so I can info them one by one. Is this the best
> solution?
>
You might consider adapting the Layout's
"Layout.format(LoggingEvent)"
In there, you can massage the LoggingEvent into several lines of your
format choice,
concatenate them and give back a multiline 'String'.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]