1

When I was introducing the Fixture module into my Grails application, I had trouble finding out how to send log messages from the application's main BootStrap.groovy and from the initialization code of my plugins.

2 Answers 2

2

I use the following log4j config in Config.groovy

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

The only change you should need to make is to set packageRoot to the root package of your app. The name/namespace of the logger that is assigned to BootStrap.groovy is grails.app.conf.BootStrap, so including this in appNamespaces ensures that it will log at the default level for the application (debug in the example above).

You don't have to do anything to get a logger instance in BootStrap.groovy, one is already provided by Grails with the name log, e.g.

class BootStrap {

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

In Grails 2.2.4:

The "log" logger is injected into the application's main BootStrap.groovy and into the plugin's descriptor (e.g.: FooGrailsPlugin.groovy)

The logger in the app's BootStrap.groovy has a name like "grails.app.BootStrap" so by enabling the appending of the "grails.app" logger in the configuration will allow displaying the messages sent through this logger.

The logger in the plugin descriptors has no package prefix, and named exactly as the descriptor class but without the groovy extension. E.g.: "FooGrailsPlugin", so it is not so easy to enable the log messages by the default injected logger. It doesn't help if you add a package definition into the top of plugin descriptor, it will not be used in the composition of the name of the logger.

Naturally, you can manually define a logger in the plugin descriptor (using a package name according to your needs) like this:

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

After this, you can enable the "yourapp.foo" logger in the application and you will see the messages sent through the plugin descriptor's manually defined logger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.