Grails on Grails Error Email Notification

Is there a way to send me information at any time when a Grails error occurs on the Grails website? What is the best way to fix this? In one place? (trying to stay dry)

And I would like to include the url that caused the error.

+5
source share
4 answers

. If you intend to catch any exception that occurs in your project and send it by mail, I use this setting, and I did it as follows:.

  • Tag created in TagLib:

    def reportError = {attrs, body ->
            String error = body()
            //error will have the Error Details, you can mail this string :)
            //mail example
            mailService.mailError(error) //just an example
            //if you want to show the error on the error page
            out << error
            //if you want to show any custom message
            out << "Error mailed, check Email"
     }
    
  • GSP views/error.gsp " < g: renderException exception =" ${exception} "/ > " " < myTagLib: reportError > < g: exceptionException =" ${exception} "/ > </myTagLib: reportError > "

...:)

, (Even in AJAX Calls), , .

, , - , , : JOBS.

:) ..

, , :)

+2

log4j, config.groovy log4j config node, , ..:

   appenders {
      appender new SMTPAppender(
         name: "smtp", to: "your email here", from: "no-reply@myserver.com",
         subject: "Grails app error", threshold: Level.ERROR,
         SMTPHost: smpt.yoursmtpserver, SMTPUsername: username,
         SMTPDebug: mail.error.debug.toString(), SMTPPassword: password,
         layout: pattern(conversionPattern:
            '%d{[ dd.MM.yyyy HH:mm:ss.SSS]} [%t] %n%-5p %n%c %n%C %n %x %n %m%n'))
   }
+2

I found that SMTPAppender does not work too well with later versions of JavaMail. I used the code here http://code.google.com/p/log4j-gmail-smtp-appender/ and added the class to my project as a groovy class, and then configured it to be used as the SMTPAppender Reply above.

0
source

Here is an example of my Config.groovy based on an article by Burt Beckwith

mail.error.server = 'smtp.yandex.ru' // or 'smtp.gmail.com'
mail.error.port = 465                // or 587 for gmail
mail.error.username = 'do.not.reply@example.com'
mail.error.password = 'site email password'
mail.error.to = 'admin.email@gmail.com'
mail.error.from = "${appName} <${mail.error.username}>"
mail.error.subject = "[Application Error] ${appName} on ${grails.serverURL}"
mail.error.starttls = true
mail.error.protocol = 'smtps' // 'smtps'  for secure protocol (SSL)
mail.error.debug = false
/*
The nice thing in this Appender is that you can send email alerts containing more than just your Exception.
 You can also add lines that were logged before the exception.
 This will make it much easier to understand the cause of your exception.
 The number of log lines that will be sent can be determine by "BufferSize" property.
 For example: if BufferSize=10, then your email will also contain the 9 lines logged before the exception.
 default 512
 */
mail.error.bufferSize = 512


// log4j configuration
log4j = {
    appenders {
        // stdout, default console appender
        appender new ConsoleAppender(name: 'stdout',
                threshold: Level.TRACE,
                layout: simple)
        // main
        appender new RollingFileAppender(name: 'main',
                threshold: Level.TRACE,
                layout: pattern(conversionPattern: '%d %c{2} %-5p %m%n'),
                file: "/var/log/${appName}/main.log",
                maxFileSize: 1024
        )
        // To send all errors or bigger level messages in email via SMTPAppender
        // Careful. If your SMTP server goes down, you may run into a thread deadlock scenario.
        System.setProperty 'mail.smtp.port', config.mail.error.port.toString()
        System.setProperty 'mail.smtp.starttls.enable', config.mail.error.starttls.toString()
        appender new SMTPAppender(name: 'smtp',
                threshold: Level.INFO,
                layout: pattern(conversionPattern: '%d{[ dd.MM.yyyy HH:mm:ss.SSS]} [%t] %n%-5p %n%c %n%C %n %x %n %m%n'),  // %5p: %d{dd MMM yyyy, HH:mm:ss} - %m%n'
                to: config.mail.error.to,
                from: config.mail.error.from,
                subject: config.mail.error.subject,
                SMTPHost: config.mail.error.server,
                SMTPUsername: config.mail.error.username,
                SMTPDebug: config.mail.error.debug.toString(),
                SMTPPassword: config.mail.error.password,
                SMTPProtocol: config.mail.error.protocol,
                bufferSize: config.mail.error.bufferSize
        )
    }

    all 'grails.app'  // Logging all for all application artifacts
    info 'grails.app.conf', // For anything under grails-app/conf such as BootStrap.groovy (but excluding filters)
            'grails.app.filters', // For filters
            'grails.app.taglib', // For tag libraries
            'grails.app.services', // For service classes
            'grails.app.controllers', // For controllers
            'grails.app.domain' // For domain entities
    error 'org.codehaus.groovy.grails.web.servlet', // controllers
            'org.codehaus.groovy.grails.web', // Grails web request processing
            'org.codehaus.groovy.grails.web.pages', // GSP
            'org.codehaus.groovy.grails.web.sitemesh', // layouts
            'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
            'org.codehaus.groovy.grails.web.mapping', // URL mapping
            'org.codehaus.groovy.grails.commons', // core / classloading
            'org.codehaus.groovy.grails.plugins', // plugins
            'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
            'org.springframework', // See what Spring is doing
            'org.hibernate', // See what Hibernate is doing
            'net.sf.ehcache.hibernate'
    // Enable Hibernate SQL logging with param values
    trace 'org.hibernate.type'
    debug 'org.hibernate.SQL'

    environments {
        development {
            // Override previous setting for 'grails.app.controller'
            all 'grails.app'
            root {
                all 'stdout'
            }
            //NOTE target/stacktrace.log would be created anyway
        }
        test {
            // Override previous setting for 'grails.app.controller'
            info 'grails.app'
            root {
                info 'stdout'
            }
            //NOTE target/stacktrace.log would be created anyway
        }
        production {
            // Override previous setting for 'grails.app.controller'
            warn 'grails.app'
            root {
                warn 'main', 'smtp'
                additivity = true
            }
        }
    }
}
0
source

All Articles