当我在我的 Grails 应用程序中引入 Fixture 模块时,我很难找到如何从应用程序的主 BootStrap.groovy 和插件的初始化代码发送日志消息。
请您参考如下方法:
我在 Config.groovy 中使用以下 log4j 配置
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'
}
}




