views:

43

answers:

2

I am new to Java World.

We have Java applicaton where it gives a specfic type of exception is there any way we can have log4j to react to specific way. Having own appender for something like MQ connection exception we need to send email to specific group.

We are in the process of customizing a Java out of the application which intern uses MQ and through exception which we need to email.

I am actually looking for the how appender will look like

A: 

Yes, you're on the right track. Implement your own Appender and only log things that match what you want to log.

Alternatively, use an existing appender (e.g. SMTPAppender) and implement/utilize an existing Filter to limit what is sent there.

Mark Peters
I think he is asking if he can catch the exception, and instead of log send email...
Nix
@Nix: Email is a form of logging, just like any other. There's an SMTPAppender that sends out e-mail.
Mark Peters
A: 

If you mean that you want to append an event only when it contains a certain class of exception, you could write a filter. Something along these lines (untested code!):

public final class ExceptionFilter extends org.apache.log4j.spi.Filter {

  private volatile String type;

  public void setType(String type) {
    this.type = type;
  }

  public String getType()
    return type;
  }

  public int decide(LoggingEvent evt) {
    Throwable t = evt.getThrowableInformation().getThrowable();
    if ((t != null) && t.getName().equals(type))
      return NEUTRAL;
    else
      return DENY;
  }

}
erickson