我希望我的Grails网络应用程序能够为每个到达最终用户的异常发送电子邮件. 基本上我正在寻找一种优雅的方式来实现相当于: try { // ... all logic/db-access/etc required to render the page is exec
基本上我正在寻找一种优雅的方式来实现相当于:
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中.