Share

August 25, 2010

Grails: Log4j Configuration Tricks/Issues

I'm one of these
"... old-school Java developers are more comfortable with log4j.xml even though the configuration file is much large."
When I started to use Grails I wanted to use my Log4j.xml and did not want to use a the Log4j XML Plugin I also was not welling to hack my Grails build Scripts.

I wanted to do the following from my log4j config in prod:

  • Console Log Level set to WARN
  • App Log File set to INFO
That said I wanted both to be in DEBUG while I'm DEV/TEST 

I also had a hard time getting this all working with the grails stand way of overriding config using environments blocks. 
Config.groovy
environments {
  development {
    log4j = {
      debug 'com.company',
              'grails',
              'groovyx.net.http'
    }
  }
}

The following is my solution for archiving what i wanted
Config.groovy
//log4j
def log4jConsoleLogLevel = Priority.WARN
def log4jAppFileLogLevel = Priority.INFO

environments {
  development {
    log4jConsoleLogLevel = Priority.DEBUG
    log4jAppFileLogLevel = Priority.DEBUG
  }
}

//////////////////////////////////////////////////////////////////
// log4j configuration
//////////////////////////////////////////////////////////////////


log4j = {
  println "Log4j consoleLevel: ${log4jConsoleLogLevel} appFile Level: ${log4jAppFileLogLevel}"

  def logLayoutPattern = new PatternLayout("%d [%t] %-5p %c %x - %m%n")

  error 'org.codehaus.groovy.grails.commons', // core / classloading
          'org.codehaus.groovy.grails.plugins', // plugins
          'org.codehaus.groovy.grails.orm.hibernate', // hibernate itg
          'org.springframework',
          'org.hibernate',
          'net.sf.ehcache.hibernate',
          'grails',
          'groovyx.net.http'

  info '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

  debug 'com.mycompany'

  appenders {
    appender new ConsoleAppender(name: "console",
            threshold: log4jConsoleLogLevel,
            layout: logLayoutPattern
    )
    appender new DailyRollingFileAppender(name: "appFile",
            threshold: log4jAppFileLogLevel,
            file: "/var/logs/tomcat/myapp/app",
            datePattern: "'.'yyyy-MM-dd",
            layout: logLayoutPattern
    )
  }

  root {
    error 'stdout', 'appFile'
    additivity = true
  }
}

Note: for a while I had a blank/empty app file and the reason for that is the log4j categories needs to be above the appenders


I also tried to use my Grails External Config  to have my config file read ConfigurationHolder.config.log4jConsoleLogLevel but the config file do not get processed until after Config.groovy is done




UPDATE: Check out Deluan's post about using an external log4j file... 
http://techbeats.deluan.com/how-to-use-an-external-log4jproperties-in-you-0 I'll create a new post on how i took his code and made changes to it as always ;)


G