11
votes

I have the following log4j config in my Grails 1.1 app

log4j = {

    // Enable Hibernate SQL logging with param values
    trace 'org.hibernate.type'
    debug 'org.hibernate.SQL'

    debug 'com.mycompany'

    appenders {
        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')
    }

    // By default, messages are logged at the error level to both the console and hibe.log
    root {
        error 'stdout', 'hibeFile'
        additivity = true
    }
}

When I run unit tests, the only logging output generated is from the Hibernate classes. I don't understand why no logging output is generated for my own classes, i.e. those under the com.mycompany namespace. Strangely, when I run integration tests, the log4j output is as expected.

If I go to the test report for a unit test, and click on the "System.out" link, I see my log messages in the following format:

DEBUG (member.InviteServiceTests): Calling getInvite with member (4517)

Notice that this is not the same pattern as that which I've specified in my log4j configuration. Furthermore, if I change the log4j config from:

debug 'com.mycompany'

to:

fatal 'com.mycompany'

I still see log messages at the debug level in the test report. It seems as though the root logger is being overriden when running unit tests? I've tried logging classes under com.mycompany using a separate logger, but this doesn't seem to make any difference

Thanks, Don

4

4 Answers

17
votes

Don,

If the classes that you are trying to log are are standard Grails classes (domain, controller, service, etc.), you should be able to use something like:

 log4j = {

    // Logging warnings and higher for all of the app
    warn 'grails.app'
    // Logging infos and higher for all controllers
    info 'grails.app.controller'
    // Logging debug and higher for the BarService
    debug 'grails.app.service.BarService'

    appenders {
    // ...as above...
    }    
    root {
    // ...as above...
    }
}

There is slightly more description at the Grails user guide section on logging.

9
votes

Since grails logging delegates to log4j, you can use -Dlog4j.debug and see how log4j is configured (reference). Perhaps another file is picked up.


There is at least one critical bug fix for logging which is targeted at 1.2 but not at 1.1.x. The configuration is similar to yours. Perhaps the messages are wrongly logged in a different file?

0
votes

You need to inject a logger in to your controller in a unit test:

mockLogging(<controller class name>, true)

The second argument says to log debug messages and below.

-1
votes

Don,

I haven't quite cracked the intricacies of logging DSL in 1.1, but this might be useful to you: Dynamic Logging Plugin allows you to turn on / off logging channels while your app is up, and also generates some config that you can then put into Conf.groovy.

Hope this helps.