Anthony
Anthony

Reputation: 35998

Logging errors in a grails application in the logs

When my Grails application crashes, it shows the error and the stacktrace on the error page because the error.gsp page has the following snippet <g:renderException exception="${exception}" />. However nothing gets logged in the log file.

How can I change this? because for the production application I plan to remove the renderException because I don't want users to see the entire stacktrace.

My log4j settings are as follows:

appenders {
    rollingFile name:'catalinaOut', maxFileSize:1024, fileName:"${System.properties.getProperty('catalina.home')}/logs/mylog.log"
}

root {
    error 'catalinaOut'
    debug 'catalinaOut'
    additivity = true
}

error  'org.codehaus.groovy.grails.web.servlet',  //  controllers
        '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',
        'org.hibernate',
        'net.sf.ehcache.hibernate',
        'grails.app'

debug   'grails.app'

}

I'm running the app in development as grails run-app

Upvotes: 2

Views: 2887

Answers (1)

moeTi
moeTi

Reputation: 3904

I use these settings for console and file based logging. You can remove stdout if you don't want/need console. Just copy all your error classes in the corresponding list.

log4j = {

    def loggerPattern = '%d %-5p >> %m%n'

    def errorClasses = [] // add more classes if needed
    def infoClasses = ['grails.app.controllers.myController'] // add more classes if needed
    def debugClasses = [] // add more classes if needed

    appenders {
        console name:'stdout', layout:pattern(conversionPattern: loggerPattern)
        rollingFile name: "file", maxFileSize: 1024, file: "./tmp/logs/logger.log", layout:pattern(conversionPattern: loggerPattern)
    }

    error   stdout: errorClasses, file: errorClasses
    info    stdout: infoClasses, file: infoClasses
    debug   stdout: debugClasses, file: debugClasses
}

Upvotes: 3

Related Questions