Grails应用程序中没有log4j输出

时间:2009-06-02 15:23:47

标签: logging grails groovy log4j

我的Grails 1.1应用程序中有以下log4j配置

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
    }
}

当我运行单元测试时,生成的唯一日志输出来自Hibernate类。我不明白为什么没有为我自己的类生成日志输出,即com.mycompany命名空间下的那些。奇怪的是,当我运行集成测试时,log4j输出正如预期的那样。

如果我转到单元测试的测试报告,然后单击“System.out”链接,我会看到以下格式的日志消息:

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

请注意,这与我在log4j配置中指定的模式不同。此外,如果我从:

更改log4j配置
debug 'com.mycompany'

为:

fatal 'com.mycompany'

我仍然在测试报告中看到调试级别的日志消息。在运行单元测试时似乎正在覆盖根记录器?我尝试使用单独的记录器在com.mycompany下记录类,但这似乎没有任何区别

谢谢, 唐

4 个答案:

答案 0 :(得分:17)

唐,

如果您尝试记录的类是标准Grails类(域,控制器,服务等),您应该可以使用以下内容:

 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...
    }
}

Grails用户指南section on logging.

中略有说明

答案 1 :(得分:9)

由于grails将委托记录到log4j,您可以使用-Dlog4j.debug并查看如何配置log4j(reference)。或许可以选择另一个文件。


至少有一个critical bug fix用于记录,其目标是1.2而不是1.1.x.配置与您的配置类似。也许这些消息被错误地记录在另一个文件中?

答案 2 :(得分:0)

您需要在单元测试中将记录器注入控制器:

mockLogging(<controller class name>, true)

第二个参数表示记录调试消息及以下内容。

答案 3 :(得分:-1)

唐,

我还没有完全破解在1.1中记录DSL的复杂性,但这可能对您有用:动态记录插件允许您在应用程序启动时打开/关闭记录通道,并生成一些配置然后可以进入Conf.groovy。

希望这会有所帮助。