我正在做一些非常像你正在做的事情。我为移动应用创建了一个单独的日志文件。我将在 Config.groovy 中包含我的整个 log4j 部分,以使其更易于理解。这将创建 4 个附加日志/var/log/grails/
- 错误、警告、信息和 mobileApp 日志;根据需要进行更改。
log4j = {
appenders {
rollingFile name:'infoLog', file:'/var/log/grails/info.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:"1MB", maxBackupIndex: 10
rollingFile name:'warnLog', file:'/var/log/grails/warn.log', threshold: org.apache.log4j.Level.WARN, maxFileSize:"1MB", maxBackupIndex: 10
rollingFile name:'errorLog', file:'/var/log/grails/error.log', threshold: org.apache.log4j.Level.ERROR, maxFileSize:"1MB", maxBackupIndex: 10
rollingFile name:'mobileAppLog', file:'/var/log/grails/mobile.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:"1MB", maxBackupIndex: 10
//UNTESTED
rollingFile name:'debugLog', file:'/var/log/grails/debug', threshold: DEBUG, maxFileSize:"1MB", maxBackupIndex: 10
}
root {
info 'infoLog','warnLog','errorLog', stdout
error()
additivity = true
}
info mobileAppLog: 'mobileAppLog', additivity:false
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'
//UNTESTED - you could remove individual entries here to get less data
debug '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'
warn 'org.mortbay.log'
debug "grails.app"
}
然后,当我需要在控制器中使用我的 mobileAppLog 时,请执行以下操作:
import org.apache.log4j.Logger
//inside controller class
def appLog = Logger.getLogger('mobileAppLog')
//To actually write to the log
appLog.info("whatever you need to write")