BuddyJoe
BuddyJoe

Reputation: 71111

Email Notification from Grails on Grails Error

Is there a way to send myself the details anytime a Grails Error happens on a Grails site? What is the best way to set this up? In one place? (trying to stay DRY)

And I would like to include the URL that produced the error.

Upvotes: 6

Views: 1992

Answers (4)

Sergey Ponomarev
Sergey Ponomarev

Reputation: 3191

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

mail.error.server = 'smtp.yandex.ru' // or 'smtp.gmail.com'
mail.error.port = 465                // or 587 for gmail
mail.error.username = '[email protected]'
mail.error.password = 'site email password'
mail.error.to = '[email protected]'
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
            }
        }
    }
}

Upvotes: 0

Scott Warren
Scott Warren

Reputation: 1591

I found the SMTPAppender didn't seem to work too well with later versions of JavaMail. I used the code from here http://code.google.com/p/log4j-gmail-smtp-appender/ and added the class into my project as a groovy class and then configured it to be used like the SMTPAppender answer above.

Upvotes: 1

Kushal Likhi
Kushal Likhi

Reputation: 570

. If you intend to catch any Exception happening on your project and mail it, well i am using such setup, and i did it in the following way: .

  1. Created a Tag in the 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"
     }
    
  2. In the GSP page views/error.gsp Replace "<g:renderException exception="${exception}"/>" Tag with "<myTagLib:reportError><g:renderException exception="${exception}"/></myTagLib:reportError>"

ALL DONE... :)

Now what is happening is, whenever an exception occours(Even in AJAX Calls) the request is redirected to the error page, there we can capture all errors.

The Only place where this setup will not work is in the case of executions which are not binded on the request, that is: JOBS.

In this method i was able to catch all unanticipated Exceptions :) ..

thats how i do, hope that Helps :)

Regards Kushal

Upvotes: 3

z.eljayyo
z.eljayyo

Reputation: 1289

You should look at log4j appenders, you could place this code in config.groovy inside log4j config node, you could also control level of reporting, conversation pattern etc:

   appenders {
      appender new SMTPAppender(
         name: "smtp", to: "your email here", from: "[email protected]",
         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'))
   }

Upvotes: 2

Related Questions