Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no log4j output in Grails app

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

like image 508
Dónal Avatar asked Jun 02 '09 15:06

Dónal


2 Answers

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.

like image 99
jkl Avatar answered Oct 19 '22 01:10

jkl


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?

like image 38
Robert Munteanu Avatar answered Oct 18 '22 23:10

Robert Munteanu