Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in Grails during Integration Testing

I've been struggling for hours trying to get logging messages displayed (either to a file or the console) when performing integration tests. I assumed I could use the following code to add a log message:

log.debug "Some useful information here"

I have commented out the following line in _GrailsWar.groovy:

target(startLogging:"Bootstraps logging") {
// do nothing, overrides default behaviour so that logging doesn't kick in    
}

as suggested here http://jira.codehaus.org/browse/GRAILS-4470

The log4j section of Config.groovy looks like this:

// log4j configuration
log4j = {
    // Example of changing the log pattern for the default console
    // appender:
    //
    appenders {
        //console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n
        //console name: 'stdout', layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n')
        //file name: 'hibeFile', file: 'hibe.log', layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n')
        file name:'file', file:'C:/Users/Christopher/Documents/NetBeansProjects/TestProject/smyh.log', append: false
        console name:'stdout'
    }

    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'

    debug 'grails.app'

    info 'grails.app'

    root {
      // change the root logger to my tomcatLog file
      error 'file', stdout
      info 'file', stdout
      warn 'file', stdout
      debug 'file', stdout
      additivity = true
    }

}

Does anyone know why I can't see my log messages during integration tests?

Many thanks for your time.

like image 824
Chris Claxton Avatar asked Jan 03 '11 16:01

Chris Claxton


1 Answers

I've finally found the solution to my own question. In order to do logging in Integration Tests, I added the following line near the top:

import org.apache.commons.logging.LogFactory

Then in the class I added the following:

def log = LogFactory.getLog(getClass()) 

This was from: http://grails.1312388.n4.nabble.com/Problems-with-Logging-td1360669.html

Then I could do logging, such as

log.debug('*** It works! ***');
like image 55
Chris Claxton Avatar answered Sep 24 '22 14:09

Chris Claxton