简体   繁体   中英

How to configure log4j for a grails plugin?

Here is my log4j config from my plugin's Config.groovy :

log4j = {       
    appenders {
        console name: "stdout",
                layout: pattern(conversionPattern: "%c{2} %m%n")
    }

    debug 'grails.app.services'
}

I have a service that logs but I am not seeing any logger print on my stdout, just to make sure I used both println and log.info but I am only seeing the println output only.

I have seen this but does not help.

How do I configure logging for a grails plugin?

You need to configure a log-level and appender for the root logger, which will be used as the defaults for all other loggers. Assuming you want to use error as the default level, and send (append) logs to the console only, do the following:

log4j = {       
    appenders {
        console name: "stdout", layout: pattern(conversionPattern: "%c{2} %m%n")
    }


    root {
        // by default, log at the ERROR level and send logs to the console
        error 'stdout'
    }

    // override the default level to DEBUG for service classes
    debug 'grails.app.services'
}
'org.codehaus.groovy.grails.plugins'

Add this code to error in log4j config. And your plugin will be loged. For example my log4j config:

log4j = {
// Example of changing the log pattern for the default console appender:
//
appenders {

    console name: 'stdout', layout: pattern(conversionPattern: '%d [%t] %-5p (%c) - %m%n')

}

root {
    info 'stdout', 'file'
    additivity = true
}
error 'org.codehaus.groovy.grails.web.servlet',        // controllers
        'org.codehaus.groovy.grails.web.pages',          // GSP
        'org.codehaus.groovy.grails.web.sitemesh',       // layouts
        'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
        'org.codehaus.groovy.grails.web.mapping',        // URL mapping
        'org.codehaus.groovy.grails.commons',            // core / classloading
        'org.codehaus.groovy.grails.plugins',            // plugins <-You nedd this one
        'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
        'org.springframework',
        'org.hibernate',
        'net.sf.ehcache.hibernate'

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM