birdy
birdy

Reputation: 9636

How to configure grails app to write logs, stack traces, and errors to a specific file

I want my grails application to log all debugs, stack traces, uncaught errors (500s etc) to be logged to a specific file myapp.log in tomcat. At the moment everything is being logged to catalina.out. Which isn't desirable as there might be other applications deployed on the Tomcat instance.

My config looks like this. I am using grails 2.2.4

log4j = {
    appenders {
        console name: "stdout", layout: pattern(conversionPattern: "%c{2} %m%n")
    }

    root {
        info "stdout"
    }

    debug "com.myapp.mypackage",
          "grails.app.controllers.com.myapp.mycontrollers"
}

Upvotes: 0

Views: 558

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

I'd put this in an environments block to make sure it only applies to the production environment. You can easily add a file appender in the appenders section, and the Grails docs show how to configure it as a rolling appender to create new files every day, or once a file has reached a maximum size, etc. 'Attach' the appender to the root level in place of the console appender:

environments {
   production {
      log4j = {
         appenders {
            console name: "stdout",
                    layout: pattern(conversionPattern: "%c{2} %m%n")

            file name: 'mylog',
                 file: '/var/logs/mylog.log',
                 layout: pattern(conversionPattern: "%c{2} %m%n")
         }

         root {
            info "mylog"
         }

         debug "com.myapp.mypackage",
               "grails.app.controllers.com.myapp.mycontrollers"

      }
   }
}

Configuring logging using the Log4j DSL is pretty confusing, but the docs explain a lot and have a bunch of helpful examples: http://grails.org/doc/latest/guide/conf.html#logging

Upvotes: 3

Related Questions