Grails 应用程序中没有 log4j 输出

发布于 2024-07-24 05:04:38 字数 1253 浏览 4 评论 0 原文

我的 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 下记录类,但这似乎没有任何区别

谢谢, 大学教师

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叶落知秋 2024-07-31 05:04:39

Don,

如果您尝试记录的类是标准 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 用户指南 有关日志记录的部分。

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.

七分※倦醒 2024-07-31 05:04:39

由于grails日志记录委托给log4j,您可以使用-Dlog4j.debug并查看log4j是如何配置的(参考)。 也许另一个文件被拾取。


至少有一个针对日志记录的关键错误修复,该修复针对的是 1.2,但不是 1.1 。X。 配置和你的类似。 也许这些消息被错误地记录在不同的文件中?

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?

心如狂蝶 2024-07-31 05:04:39

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

mockLogging(<controller class name>, true)

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

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.

ま昔日黯然 2024-07-31 05:04:39

Don,

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

希望这可以帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文