logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Roberto Giménez" <chila...@gmail.com>
Subject Re: New Lines
Date Tue, 18 Jul 2006 18:28:45 GMT
I am calling an encapsulated layout, it is a PatternLayout. The special case
of empty message but throwable information is not covered, i'll fix that,
thanks.
The use of a StringTokenizer for every single line is not efficient at all.
Retrieving the Logger probably isn't either, due the table lookup. About the
recursion... well... I don't think that the time and memory it consumes is
very significative.

But I think that the creation and use of the StringTokenizer for every
single line, the creation of LoggingEvents for every token line, and the
Logger lookup are ineficient methods. I think that what I have to do is to
create the output string by begining it with the prefix, and then copying
each character of the message and everytime I find a nl/cr character I'll
just place the prefix before the next character to be copied. I can return
this string and avoid the creation of another logging event since I don't
use any of the other members contained in the event. I'll have to create the
prefix by hand thou, but the prefix is not so complex anyway. I'm using
jdk1.3.1 and here Throwables cant return the stack frames, that's why I have
to print the stacktrace into a ByteArrayOutputStream, this is also
inneficient but there's nothing I can do about it.


2006/7/18, David Tonhofer <d.tonhofer@m-plify.com>:
>
> 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: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message