我希望我的 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);
}
最佳答案
原来这个问题是 answered on the Grails mailing list几天前。
解决方案是将以下内容添加到 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/-folder 中。
关于exception - 实时 Grails Web 应用程序中的邮件异常日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/635590/