grails log4j diffrerent appenders according files

2019-02-15 20:52发布

I would like to log to different appenders depending on the module...

I have 3 appenders: the console, a rolling file to log the controller and services, and another rolling file to log some stuff in jobs. I want to log the code of the job only for its rolling file and to log the controllers and services only with the other rolling file.

So this is my grails log4j configuration:

development {   
    def catalinaBase = System.properties.getProperty('catalina.base')
    if (!catalinaBase) catalinaBase = '.'
    def logDirectory = "${catalinaBase}/logs/AmbienticWebsite"


    log4j = {

        appenders {
            console name:'stdout', layout:pattern(conversionPattern: '%c{2} [%p] - %m%n')
            appender new DailyRollingFileAppender(
                    name: "rollingFileGrailsApp",
                    file: "${logDirectory}/GrailsApp.log",
                    datePattern: "'.'yyyy-MM-dd",
                    layout: pattern(conversionPattern: commonPattern)
            )
            appender new DailyRollingFileAppender(
                name: "rollingFileImport",
                file: "${logDirectory}/Imports.log",
                datePattern: "'.'yyyy-MM-dd",
                layout: pattern(conversionPattern: commonPattern)
            )
        }

        root {
            error 'stdout', 'rollingFileImport', 'rollingFileGrailsApp' // both stdout and AmbienticWebsite_dev.log are filled by logging information
            additivity = false
        }

          debug rollingFileImport: 'ambienticwebsite.EventImportJob',
                                    'time2marketing.time2marketingImportService',
                                    'eventImportData.DiscomImportDataService',
                                    'eventImportData.EventImportService'

          info rollingFileGrailsApp: 'ambienticwebsite', 
                'ambienticwebsite.jobManagement.AmbienticJobListener',
                'BootStrap',
                'grails.app.controllers',
                'grails.app.services'       
    }
}

With this configuration, the logs are written to the two rolling files and stdout. If I delete the rolling appenders from the root, the rolling file stays empty, even if the appenders are specified for the group of files.

Does anyone have advice to separate the log into the appenders?

标签: grails log4j
1条回答
forever°为你锁心
2楼-- · 2019-02-15 21:20

I think you need to add additivity: false to the custom appenders.

debug rollingFileImport: [foo, bar, baz]
      additivity: false

info rollingFileGrailsApp: [foo1, bar1, baz1]
     additivity: false

Refer Logger Inheritance in grails.

查看更多
登录 后发表回答