views:

223

answers:

1

Question:

  • How do I configure Grails to send an e-mail with all log4j error messages (including exceptions) generated in the production environment?

Worth noting:

+3  A: 

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

log4j = {
  ...
  appenders {
    console name:'stdout', layout:pattern(conversionPattern: '[%r] %c{2} %m%n')
    if (grails.util.Environment.current == grails.util.Environment.PRODUCTION) {
      def patternLayout = new org.apache.log4j.PatternLayout()
      patternLayout.setConversionPattern("[%r] %c{2} %m%n")
      def mailAppender = new org.apache.log4j.net.SMTPAppender()
      mailAppender.setFrom("[email protected]")
      mailAppender.setTo("[email protected]")
      mailAppender.setSubject("An log4j error has been generated in the production environment")
      mailAppender.setSMTPHost("your-smtp-host")
      mailAppender.setLayout(patternLayout)
      appender name:'mail', mailAppender
    }
  }
  root {
    error 'stdout', 'mail'
    additivity = true
  }
  ...
}

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

This configuration has been tested and it works as intended.

knorv