views:

40

answers:

1

Hi,

I've got few issues with Grails logging during test [running grails test-app, Grails 1.3.5]:

1

I've got some debug/info logging in my application and it works fine when running the app [grails run-app]. However when I want to test my app, none of it is written to the System.out/System.err files nor to file appender. How can I enable it?

I've got log.debug() and log.info() lines in my domain classes. in controllers and in classes located in src/groovy.

When I wanted to enable logging during test I just copied settings from my dev environment, and changed root logger from info to debug:

    appenders {
        file name: 'file', file: 'mylog.log'
    }

    root {
        debug 'stdout', 'file'
    }

    debug 'grails.app'


    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'

As I've said earlier. Everything works fine if I run the app in dev environment. It's only the test that I cannot see my logs.

Something to mention here: I can see my log.info() lines specified in the Test classes.

2 I cannot specify log.debug in Test classes. I'm getting missing method exception when trying to do log.debug. log.info works just fine. How come? I thought it's the same injection as within controllers/domains.

3 All information logged in Test classes during test are sent to System.err instead of System.out. Is grails even using the log4j configuration from Config.groovy?

Thanks in advance, Krystian

A: 

what I do is just define my own logger and inject it into the controller or service in my test case.

    BasicConfigurator.configure() 
    LogManager.rootLogger.level = Level.DEBUG
    log = LogManager.getLogger("MailService")

    MailService.class.metaClass.getLog << {-> log}

I do not believe the log property is injected into test classes

I have also done this before which turned out to be more of a hack but worked

    def logger = new Expando(
            debug: { println it },
            info: { println it },
            warn: { println it },
            error: { println it })
    MailService.metaClass.getLog = {-> logger }
    MailServiceService.metaClass.getLog = {-> logger }
    MailIngestJobTests.metaClass.getLog = {-> logger }

Final Code Solution Here

Aaron Saunders
Thanks Aaron, unfortunately I can't get this to work. I just put your code [the hack with def logger ... ] into a test method in my TestCase and well.. I can't find the output anywhere. Nor for domain, nor controller nor for a class in src/groovy folder :/
Krystian
look in target/test-reports/plain
Aaron Saunders
can you post the testcase code
Aaron Saunders
Hi Aaron. Last night it was 3am in here and I was not thinking straight ;) I don't want to implement println, I need full control that is given to me by Log4J. Print lines are not enough. And the code is in here: http://pastebin.com/Jgv1uj5R
Krystian
@Krystian see edit above, think you will have to make sure Log4j is is imported
Aaron Saunders
Aaron, I've tried different ways like putting the code into bootstrap, into setup of test case, into the test method of test case and well... the only logging I can see is from test case itself. The code: http://pastebin.com/yBqhRnrx I'm wondering if this is because it's the commons logging that is used by grails [at least 1.3.5] during testing and not log4j?
Krystian
i put this code in a simple test case on a domain object and it is working... go back to a default log4j config... and see if that makes a difference
Aaron Saunders
Hi Aaron. You were right. My test project was so screwed up by different changes I had to start over. Once I did it worked fine. I didn't like the lack of configuration so I wrote this class: http://pastebin.com/tM28RCcV Please add a link to your answer as it might help others [feel free to edit it as it's not very groovy and lacks many things... however works fine]. I use this class to inject logging in setUp methods of my test cases, however I am sure it could be used elsewhere to inject all classes once and not every time a test is run. I've tried doing it in bootstrap but it didn't work.
Krystian
someone else agrees with the approach for log configuration see link and JIRA http://techbeats.deluan.com/how-to-use-an-external-log4jproperties-in-you-0 http://jira.codehaus.org/browse/GRAILS-1964
Aaron Saunders