Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Grails BootStrap.groovy和插件描述符发送日志消息_Grails_Logging_Plugins_Bootstrapping - Fatal编程技术网

从Grails BootStrap.groovy和插件描述符发送日志消息

从Grails BootStrap.groovy和插件描述符发送日志消息,grails,logging,plugins,bootstrapping,Grails,Logging,Plugins,Bootstrapping,当我将Fixture模块引入Grails应用程序时,我很难找到如何从应用程序的主BootStrap.groovy和插件的初始化代码发送日志消息。在Grails 2.2.4中: “日志”记录器被注入应用程序的主BootStrap.groovy和插件的描述符(例如:FooGrailsPlugin.groovy) 应用程序的BootStrap.groovy中的记录器的名称类似于“grails.app.BootStrap”,因此通过在配置中启用“grails.app”记录器的附加,将允许显示通过此记录器

当我将Fixture模块引入Grails应用程序时,我很难找到如何从应用程序的主BootStrap.groovy和插件的初始化代码发送日志消息。

在Grails 2.2.4中:

“日志”记录器被注入应用程序的主BootStrap.groovy和插件的描述符(例如:FooGrailsPlugin.groovy)

应用程序的BootStrap.groovy中的记录器的名称类似于“grails.app.BootStrap”,因此通过在配置中启用“grails.app”记录器的附加,将允许显示通过此记录器发送的消息

插件描述符中的记录器没有包前缀,其名称与描述符类完全相同,但没有groovy扩展。例如:“FooGrailsPlugin”,因此通过默认注入的记录器启用日志消息并不容易。如果将包定义添加到插件描述符的顶部,它将不会在记录器名称的组合中使用

当然,您可以在插件描述符中手动定义记录器(根据需要使用包名),如下所示:

private static final log = LogFactory.getLog("yourapp.foo.FooGrailsPlugin")

在此之后,您可以在应用程序中启用“yourapp.foo”记录器,您将看到通过插件描述符的手动定义记录器发送的消息。

我在
config.groovy中使用以下log4j config

log4j = {
    appenders {
        console name: 'consoleAppender', layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n')
    }

    root {
        // define the root logger's level and appenders, these will be inherited by all other loggers
        error 'consoleAppender'
    }

    // change the default log level for classes in our app to DEBUG
    def packageRoot = 'com.example.myapp'

    def appNamespaces = [
            packageRoot,
            "grails.app.conf.$packageRoot",
            "grails.app.filters.$packageRoot",
            "grails.app.taglib.$packageRoot",
            "grails.app.services.$packageRoot",
            "grails.app.controllers.$packageRoot",
            "grails.app.domain.$packageRoot",
            "grails.app.conf.BootStrap"
    ]

    // statements from the app should be logged at DEBUG level
    appNamespaces.each { debug it }
}
您需要做的唯一更改是将
packageRoot
设置为应用程序的根包。分配给
BootStrap.groovy
的记录器的名称/命名空间是
grails.app.conf.BootStrap
,因此将其包含在
appNamespaces
中可确保它将以应用程序的默认级别进行日志记录(在上面的示例中进行调试)

BootStrap.groovy
中获取记录器实例不需要做任何事情,Grails已经提供了一个名为
log
的记录器实例,例如

class BootStrap {

    def init = { servletContext ->
        log.debug 'hello bootstrap'
    }
}