10 June 2014

Out of the box, the Grails framework programmatically configures a file-based logging appender that produces a log file named stacktrace.log. This log contains any errors generated by the Grails application. One of its problems is that this appender does not use a rolling file appender and therefore can grow without bound. Typically, I configure my own applications to use a rolling file appender which makes the stacktrace.log file superfluous, as all errors are also logged to my custom appender. You can remove this appender at runtime by adding some magic to your application’s grails-app/conf/Bootstrap.groovy file:

Boostrap.groovy
import org.slf4j.LoggerFactory

import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.core.util.StatusPrinter

class BootStrap {

    def init = { servletContext ->
        // Remove the Grails stack trace log appender, as it does not use a rolling appender.
        removeStackTraceLogAppender()
    }

    def destroy = {
    }

    /**
     * Removes the 'stacktraceLog' appender added by Grails.  This appender does NOT use a
     * rolling file appender and can grow to astronomical sizes if left unattended.  Removing
     * this log file does not remove any data, as the errors logged to it also get logged
     * to the console appender.
     */
    private def removeStackTraceLogAppender() {
        def logger = LoggerFactory.getLogger('StackTrace')
        if(logger) {
            def stackTraceAppender = logger.getAppender('stacktraceLog')
            if(stackTraceAppender) {
                if(logger.detachAppender(stackTraceAppender)) {
                    println "Grails 'stacktraceLog' log appender removed from configuration."
                }
            }
        }
    }
}

The code above asks the SLF4J API for the StackTrace logger, which is created programmatically by the Grails framework. From that logger, the stacktraceLog appender is retrieved and detached, to ensure that any information sent to the StackTrace logger does not get appended to the stacktrace.log file. This helps to prevent this log file from growing in an unbounded manner.

comments powered by Disqus