Or you can use what I wrote to handle that:

import org.apache.log4j.*;
import org.apache.log4j.spi.*;

/**
 * Extends PatternLayout to also handle Throwable items attached to
 *   LoggingEvents.
 */
public class PatternWithThrowableLayout extends PatternLayout {
  /**
   * Constructs a PatternWithThrowableLayout using the
DEFAULT_LAYOUT_PATTERN.
   * 
   * The default pattern just produces the application supplied message.
   */
  public PatternWithThrowableLayout() {
    super();
  }

  /**
   * Constructs a PatternWithThrowableLayout using the supplied conversion
pattern.
   * 
   * @param pattern
   */
  public PatternWithThrowableLayout(String pattern) {
    super(pattern);
  }

  /**
   * Produces a formatted string as specified by the conversion pattern.<p>
   * 
   * If there's a Throwable attached to the LoggingEvent, its StackTrace is
   *   appended.
   * 
   * @param event    the event to log
   * 
   * @return the formatted String
   */
  public String format(LoggingEvent event) {
    String str = super.format(event);

    if (event.throwable != null) {
      // for storing stack trace
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      PrintStream ps = new PrintStream(os);  // printing destination
      event.throwable.printStackTrace(ps);
      return str+os.toString();
    }
    else {
      return str;
    }
  }

  /**
   * The PatternWithThrowableLayout handles the throwable contained within
   *   {@link LoggingEvent LoggingEvents}. Thus, it returns
   *   <code>false</code>.
   * 
   * @return FALSE
   */
  public boolean ignoresThrowable() {
    return false;
  }
}

-Jim Moore

"Pinky, you've left the lens cap of your mind on again." - The Brain.


-----Original Message-----
From: Ceki Gülcü [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 06, 2001 12:53 PM
To: LOG4J Users Mailing List
Subject: Re: SMTPAppender and exceptions



Omer,

This is a bug! The SMTPAppender assumes that its layout takes care of the
exception, which is not the case with the PatternLayout. I suggest that you
use the HTMLLayout with SMTPAppender until the bug gets fixed. Ceki

At 12:48 06.02.2001 -0500, you wrote:
>Hi,
>
>I'm using SMTPAppender to send out an e-mail whenever an error event is
>logged. I'm finding that when I log an exception like this
>
>catch (Exception e)
>{
>    cat.error("Oops!", e);
>}
>
>the e-mail will only have the "Oops!" line, whereas my rotating file log
>will also have the stack trace. 
>
>I'd like to have the stack trace in the e-mail as well. Is there a
>simple way to do that? 
>
>Thanks,
>
>        Omer van der Horst Jansen
>
>[EMAIL PROTECTED]
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]

----
Ceki Gülcü           e-mail: [EMAIL PROTECTED] (preferred)
av. de Rumine 5              [EMAIL PROTECTED]
CH-1005 Lausanne          
Switzerland            Tel: ++41 21 351 23 15


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to