I am new to Java World.
We have a Java application where it gives a specific 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 emai开发者_Python百科l 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 how the appender will look like
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.
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;
}
}
精彩评论