Grails 1.1 logging

Grails 1.1 has changed logging setup completely, introducing a new DSL closure. For the most part it’s cool, but the syntax looks the same sometimes for both log level and appender assignments. I’ve updated my previous posts on debug logging and stacktrace.log. For the record, included below is the complete log4j closure I’m now using for a new Grails 1.1 project (or download it). Append this to the default Config.groovy file.

environments {
    development {
        log4j = {
            // in development mode, let's see all my log messages
            debug 'grails.app'
        }
    }

    production {
        def catalinaBase = System.properties.getProperty('catalina.base')
        if (!catalinaBase) catalinaBase = '.'   // just in case
        def logDirectory = "${catalinaBase}/logs"

        log4j = {
            appenders {
                // set up a log file in the standard tomcat area; be sure to use .toString() with ${}
                rollingFile name:'tomcatLog', file:"${logDirectory}/${appName}.log".toString(), maxFileSize:'100KB'
                'null' name:'stacktrace'
            }

            root {
                // change the root logger to my tomcatLog file
                error 'tomcatLog'
                additivity = true
            }

            // example for sending stacktraces to my tomcatLog file
            //error tomcatLog:'StackTrace'

            // set level for my messages; this uses the root logger (and thus the tomcatLog file)
            info 'grails.app'
        }
    }
}

The newest bit here is setting the log file to Tomcat’s log directory for production. Without that, Grails/Log4J will try to open files in the place where tomcat starts up (e.g. / or someplace you probably can’t write files). Also, the arguments to appenders (and layouts) are sent directly to Apache BeanUtils so must be Java Strings — use .toString() on your GStrings (a string with variable interpolation).

(I’ve also updated several of my other Grails posts for 1.0.4 and 1.1.)

Edit: fixed a syntax error that crept in between testing and posting.

Tags: ,

8 Responses to “Grails 1.1 logging”

  1. chris says:

    Hi,

    I have tried exactly the same thing but it does not work. When I deploy on tomcat. tomcat says all is fine etc but I cannot find any logs. Also when I try to access my app through tomcat I get a 404 error.

  2. jja says:

    The 404 error is suspicious - perhaps that’s not a logging issue, but your basic installation is not working at all. Is it *really* deployed properly in Tomcat? Are you running the manager webapp and does it show your app as running? Is it an issue with communication between Apache httpd (webserver) and Tomcat? And the first question really is: does your app work in grails dev mode? i.e. running from the command line like “grails run-app”

  3. Pieter says:

    Thanks, this helped me get my logs in Tomcat!

  4. Justin says:

    Thanks so much for this.

    The ‘log4jdsl.txt’ got my logging up and running and then I was able to fix my problem in no time.

    Not sure why they like to keep us guessing in the dark for hours on end though. It’s not exactly how I planned to spend my bank holiday!

  5. Thank you very much. I had a lot of issues with log4j before. With your description everything runs fine!

    Best regards,
    Conrad

  6. Thank you! This saved my day.

  7. Edwardo says:

    Is there a way to set the catalina.base automatically? I get null when i do getProperty. I know I can set it in a shell window with export. Is there another alternative location to place the log files?

  8. jja says:

    @Edwardo: catalina.base is originally set in the Tomcat startup script catalina.sh or catalina.bat via a -D argument to the java executable. Sorry for the delay in replying.

Leave a Reply