Roberto Giménez wrote:
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.
<...>
public String format(LoggingEvent event)
{
String message = (String)event.getMessage();
String ret = "";
if (message.length() == 0)
// CALL ENCAPSULATED LAYOUT
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
);
// CALL ENCAPSULATED LAYOUT
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);
// RECURSIVE CALL
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.
Looks ok, you are calling an encapsulated layout, right? The special case of
empyt message but throwable information is not correctly covered though.
One could maybe nitpick about the efficiency of using a StringTokenizer
every time
and retrieving the Logger at each line and the fact that you call
recursively
instead of just processing right there .. :-=
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]