grails separating info logging

2019-07-03 04:16发布

问题:

I am trying to separate the info, warn and errors log, i did the below configuration, but on info file continue to log the other types of log on the same file, i already tried to search on the other topics but i did not find a solution, anyone can help ?

def logLayoutPattern = new PatternLayout("%d{yyyy-MM-dd/HH:mm:ss.SSS} %x %-5p %c{2} - %m%n")
appenders {
     appender new DailyRollingFileAppender(name: "perfil",threshold: Level.INFO,file: "/tmp/logs/file_perfil.log",datePattern: "'.'yyyy-MM-dd",layout: logLayoutPattern)
     appender new DailyRollingFileAppender(name: "errors",threshold: Level.ERROR,file: "/tmp/logs/file_errors.log",datePattern: "'.'yyyy-MM-dd",layout: logLayoutPattern)
     appender new DailyRollingFileAppender(name: "warn",threshold: Level.WARN,file: "/tmp/logs/file_warn.log",datePattern: "'.'yyyy-MM-dd",layout: logLayoutPattern)
}

info perfil: ["grails.app.controllers.com.app.PerfilController"]
warn warn: 'grails.app'

error errors: ['org.codehaus.groovy.grails.web.servlet',
       'org.codehaus.groovy.grails.web.pages',
       'org.codehaus.groovy.grails.web.sitemesh',
       'org.codehaus.groovy.grails.web.mapping.filter',
       'org.codehaus.groovy.grails.web.mapping',
       'org.codehaus.groovy.grails.commons',
       'org.codehaus.groovy.grails.plugins',
       'org.codehaus.groovy.grails.orm.hibernate',
       'org.springframework',
       'org.hibernate',
       'net.sf.ehcache.hibernate']
root {
    error 'errors'
    additivity = false
    warn 'warn'
    additivity = false
    info 'perfil'
    additivity = false
}

回答1:

You need to add additivity: false on the custom appenders instead of the root logger.

Something like this would suffice:

info perfil: ["grails.app.controllers.com.app.PerfilController"]
     additivity: false

warn warn: 'grails.app'
     additivity: false

error errors: ['org.codehaus.groovy.grails.web.servlet',
       'org.codehaus.groovy.grails.web.pages',
       'org.codehaus.groovy.grails.web.sitemesh',
       'org.codehaus.groovy.grails.web.mapping.filter',
       'org.codehaus.groovy.grails.web.mapping',
       'org.codehaus.groovy.grails.commons',
       'org.codehaus.groovy.grails.plugins',
       'org.codehaus.groovy.grails.orm.hibernate',
       'org.springframework',
       'org.hibernate',
       'net.sf.ehcache.hibernate']
     additivity: false

root {
    error 'errors'
    warn 'warn'
    info 'perfil'
}

All the loggers inherit from the root by default and additivity is true by default.