简体   繁体   English

log4j异常处理

[英]log4j Exception handling

I am new to Java World. 我是Java World的新手。

We have a Java application where it gives a specific type of exception. 我们有一个Java应用程序,其中提供了特定类型的异常。 Is there any way, we can have log4j to react to specific way. 有什么办法,我们可以让log4j对特定的方法做出反应。 Having own appender for something like MQ connection exception, we need to send email to specific group. 对于诸如MQ连接异常之类的东西,拥有自己的附加程序,我们需要向特定的组发送电子邮件。

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. 我们正在从应用程序中自定义Java的过程中,该应用程序使用MQ进行实习生,并且需要通过电子邮件发送异常。

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. 实现自己的Appender,仅记录与您要记录的内容匹配的内容。

Alternatively, use an existing appender (eg SMTPAppender ) and implement/utilize an existing Filter to limit what is sent there. 或者,使用现有的附加程序(例如SMTPAppender )并实现/利用现有的Filter来限制在此发送的内容。

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;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM