Mailing Exception logs in a live Grails webapp

前端 未结 3 480
有刺的猬
有刺的猬 2021-02-04 21:24

I\'d like my Grails web-app to send an e-mail for each exception that reaches the end-user.

Basically I\'m looking for a elegant way to achieve something equivalent to:<

相关标签:
3条回答
  • 2021-02-04 21:40

    Turns out this exact question was answered on the Grails mailing list a couple of days ago.

    The solution is to add the following to the log4j-section of Config.groovy:

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

    Plus adding sun-javamail.jar and activation.jar to the lib/-folder.

    0 讨论(0)
  • 2021-02-04 21:42

    You could also take a look at exceptionHandler mechanism provided by Grails; I find it very simple; yet powerful enough to take care of all my custom & clean exception handling needs. Haven't tested this approach with 1.1 so far; but works very well with 1.0.3.

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

    Detailed blog here :

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

    0 讨论(0)
  • 2021-02-04 21:49

    Assuming you can do this from groovy, you'll want to use a logging framework such as log4j for this, which has loggers that can append log data to a database, send email, etc.

    0 讨论(0)
提交回复
热议问题