Dec 172009
 

Logging is a better way to debug and provide messages than slapping println statements around. Here is an example of a grails config for log4j. This will also place the log file in with the Tomcat logs when you deploy.

Config.groovy:

 
/**
* Directory configuration.
* Pickup the Tomcat/Catalina directory else use the target or current dir.
*/
def fs = File.separator // Local variable.
globalDirs.targetDir = new File("target${fs}").isDirectory() ? "target${fs}" : ''
globalDirs.catalinaBase = System.properties.getProperty('catalina.base')
globalDirs.logDirectory = globalDirs.catalinaBase ? "${globalDirs.catalinaBase}${fs}logs${fs}" : globalDirs.targetDir
globalDirs.workDirectory = globalDirs.catalinaBase ? "${globalDirs.catalinaBase}${fs}work${fs}" : globalDirs.targetDir
globalDirs.searchableIndexDirectory = "${globalDirs.workDirectory}SearchableIndex${fs}${appName}${fs}"
 
/**
 * Log4j configuration.
 * Causing this file to reload (e.g. edit+save) may break the appLog destination
 * and further logs will be written to files or directories like "[:]".
 * For more info see http://logging.apache.org/log4j/1.2/manual.html
 * For log levels see http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
 * Basic log levels are ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
 */
log4j = {
    appenders {
        // Use if we want to prevent creation of a stacktrace.log file.
        'null' name:'stacktrace'
 
        // Use this if we want to modify the default appender called 'stdout'.
        console name:'stdout', layout:pattern(conversionPattern: '[%t] %-5p %c{2} %x - %m%n')
 
        // Custom log file.
        rollingFile name:"appLog",
                        file:"${globalDirs.logDirectory}${appName}.log".toString(),
                        maxFileSize:'300kB',
                        maxBackupIndex:1,
                        layout:pattern(conversionPattern: '%d{[EEE, dd-MMM-yyyy @ HH:mm:ss.SSS]} [%t] %-5p %c %x - %m%n')
    }
 
    // This is for the built-in stuff and from the default Grails-1.2.1 config.
    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'
 
    warn   'org.mortbay.log' // Jetty
 
    error 'grails.app' // Set the default log level for our app code.
    info 'grails.app.bootstrap' // Set the log level per type and per type.class
    error 'grails.app.service.AuthService'
    error 'grails.app.service.NavigationService'
    error 'grails.app.service.com.zeddware.grails.plugins.filterpane.FilterService'
    info 'org.codehaus.groovy.grails.plugins.searchable'
    //info 'org.compass'
    error 'grails.app.task' // Quartz jobs.
    info 'grails.app.task.InventoryIndexJob'
 
    // Move anything that should behave differently into this section.
    switch(environment) {
        case 'development':
            // Configure the root logger to output to stdout and appLog appenders.
            root {
                error 'stdout','appLog'
                additivity = true
            }
            //debug "org.hibernate.SQL"
            debug 'grails.app.service'
            debug 'grails.app.controller'
            break
        case 'test':
            // Configure the root logger to only output to appLog appender.
            root {
                error 'stdout','appLog'
                additivity = true
            }
            debug 'grails.app.service'
            debug 'grails.app.controller'
            break
        case 'production':
            // Configure the root logger to only output to appLog appender.
            root {
                error 'appLog'
                additivity = true
            }
            warn 'grails.app.service'
            warn 'grails.app.controller'
            debug 'grails.app.service.AssetCsvService'
            debug 'grails.app.service.PersonCsvService'
            debug 'grails.app.service.InventoryCsvService'
            debug 'grails.app.service.AssetTreeService' /// @todo: remove after testing.
            break
    }
}
 
/**
 * Environment specific configuration.
 */
environments {
 
    production {
        grails.serverURL = "http://www.changeme.com" // Set serverURL stem for creating absolute links.
    }
 
    development {
        grails.serverURL = "http://localhost:8080/${appName}" // Set serverURL stem for creating absolute links.
    }
 
    test {
        grails.serverURL = "http://localhost:8080/${appName}" // Set serverURL stem for creating absolute links.
    }
 
} // end environments

Note that a switch is used to keep the log config all together and that things need to be moved in/out of the switch as overriding does not play nicely.

[suffusion-the-author display='first-name'] [suffusion-the-author display='last-name']:


Thank you for visiting.
Do look around.
Do leave a comment.

  6 Responses to “Grails log4j config”

  1. what if I have the same configuration for more than one grails apps on my tomcat.
    When I undeploy one, its invalidating the log4j appenders and throwing errors to initialize log4j properly.

    Any suggestions?

  2. I have seen Tomcat throw errors during shutdown or undeploy even with a single application. This often has something to do with commons-logging and where the shared jars are placed. Google for this and try a few of the suggested configurations. Does the logging of the still deployed applications stop? Many times I have seen multiple separate Tomcat instances recommended, I guess for this and other reasons, I have personally never had much luck with ‘undeploy’ and always shut Tomcat down completely when changing applications. Hope that helps.

  3. Thank you for sharing this. It helped!

  4. Grails log4j config…

    Logging is a better way to debug and provide messages than slapping println statements around. Here is an example of a grails config for log4j. This will also place the log file in with the Tomcat logs when you deploy.

  5. I’d like to buy you a beer! maxFileSize: Who knew! You did!

  6. I happily take cider and fine single malt scotch ๐Ÿ™‚

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)