Grails logging: Multiple loggers and appenders

2019-05-18 14:57发布

I am trying to configure a Grails (2.2.0) app to have a log file for a specific package and then use the console output for everything else. I also want to make sure that nothing that goes to the log file also goes to the console (keep them totally separate). Reading through the docs I am still a bit confused on how to get this done. Could someone help out with an example that accomplishes this (using the log4j DSL)?

1条回答
疯言疯语
2楼-- · 2019-05-18 15:35

I got this working with help from this question. Here's what I ended up with in my Config.groovy:

log4j = {
    appenders {
        console name: 'stdout', layout: pattern(conversionPattern: '%d{yyyy-MM-dd/HH:mm:ss.SSS} [%t] %x %-5p %c{2} - %m%n')

        rollingFile name: 'extraAppender',
                conversionPattern: '%d{yyyy-MM-dd/HH:mm:ss.SSS} [%t] %x %-5p %c{2} - %m%n',
                maxFileSize: 1024,
                file: '/tmp/logs/extra.log'
    }

    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
           'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
           'org.springframework',
           'org.hibernate',
           'net.sf.ehcache.hibernate'

    trace  additivity: false, extraAppender: 'extraLogger'
}

Then in the class that's supposed to use the extraLogger, I just go the logger like this:

def extraLogger = LoggerFactory.getLogger('extraLogger')

That lets me log more things to the console and some other specific things to another log file, without any of those specific things ending up in the console log.

查看更多
登录 后发表回答