4

我希望我的 Grails Web 应用程序为到达最终用户的每个异常发送一封电子邮件。

基本上我正在寻找一种优雅的方式来实现相当于:

  try {
      // ... all logic/db-access/etc required to render the page is executed here ...
  }
  catch (Exception e) {
      sendmail("exception@example.com", "An exception was thrown while processing a http-request", e.toString);
  }
4

3 回答 3

6

事实证明,这个确切的问题几天前在 Grails 邮件列表中得到了回答。

解决方案是将以下内容添加到 Config.groovy 的 log4j 部分:

log4j {
    ...
    appender.mail='org.apache.log4j.net.SMTPAppender'
    appender.'mail.To'='email@example.com'
    appender.'mail.From'='email@example.com'
    appender.'mail.SMTPHost'='localhost'
    appender.'mail.BufferSize'=4096
    appender.'mail.Subject'='App Error'
    appender.'mail.layout'='org.apache.log4j.PatternLayout'
    appender.'mail.layout.ConversionPattern'='[%r] %c{2} %m%n'
    rootLogger="error,stdout,mail"
    ...
    // rootLogger="error,stdout" (old rootLogger)
}

加上将 sun-javamail.jar 和 activation.jar 添加到 lib/-文件夹。

于 2009-03-11T20:02:40.520 回答
0

假设您可以从 groovy 执行此操作,您将需要为此使用诸如log4j之类的日志框架,它具有可以将日志数据附加到数据库、发送电子邮件等的记录器。

于 2009-03-11T17:47:37.023 回答
0

你也可以看看Grails 提供的exceptionHandler机制;我觉得很简单;但足够强大,可以满足我所有自定义和干净的异常处理需求。到目前为止,还没有用 1.1 测试过这种方法;但适用于 1.0.3。

class BootStrap {

   def exceptionHandler

   def init = { servletContext ->
      exceptionHandler.exceptionMappings =
        [ 'NoSuchFlowExecutionException' :'/myControler/myAction',
        'java.lang.Exception' : '/myController/generalAction']
   }

  def destroy = { }
}

详细博客在这里:

http://blog.bruary.net/2008/03/grails-custom-exception-handling.html

于 2009-03-12T01:13:05.253 回答