我想将 BootStrap.groovy 文件中的一些信息记录到控制台和日志文件中。输出报告了一些配置信息,并标记了应用程序从配置到执行阶段的过渡。我遇到的问题是仅在控制台上看到所需的输出,而不是在日志文件中。我的环境包括
- 圣杯 2.3.8
- 红帽企业 Linux 工作站 6.5 版
我提供了两个代码片段来展示我在做什么。它们也很容易插入一个空的 Grails 项目(不需要创建域类或控制器来查看我所看到的行为)。当我运行“ grails run-app ”时,我希望块的开发部分environments
将 BootStrap 输出配置到两个位置。除了让日志记录到这两个位置之外,如果您对我的 log4j 配置有任何一般性建议,也将不胜感激。
我修改了我的BootStrap.groovy:
import grails.util.Environment
class BootStrap {
def grailsApplication
def init = { servletContext ->
log.info """
tryLoggingInGrails2-3-8 configuration {---------------- ${new Date()}
environment : ${Environment.current.name}
dataSource.username : ${grailsApplication.config?.dataSource?.username}
dataSource.url : ${grailsApplication.config?.dataSource?.url}
------------------------------------------------------}"""
}
def destroy = {
}
}
在Config.groovy中,log4j 配置部分是:
// ----------------------------- Start Config.groovy snippet
// log4j configuration
def catalinaBase = System.getProperty( 'catalina.base' )
if ( !catalinaBase ) catalinaBase = './target' // just in case
def logDirectory = "${catalinaBase}/logs"
def consoleLevelThreshold = org.apache.log4j.Level.WARN
def fileLevelThreshold = org.apache.log4j.Level.INFO
environments {
development {
consoleLevelThreshold = org.apache.log4j.Level.DEBUG
fileLevelThreshold = org.apache.log4j.Level.DEBUG
}
}
// Inside the log4j closure, use '${myAppName}' instead of '${appName}'
def myAppName = appName
log4j = {
appenders {
// This 'null' prevents the empty stacktrace.log file from being created
// in the default location, where we may not have permission to write
// in a production tomcat.
'null' name: 'stacktrace'
console name: 'stdoutAppender',
threshold: consoleLevelThreshold,
layout: pattern( conversionPattern: '%-5p %c{2} %m%n' )
file name: 'fileAppender',
file: "${logDirectory}/${myAppName}.log",
threshold: fileLevelThreshold,
layout: pattern( conversionPattern: '%-4r [%t] %-5p %c %d %x - %m%n' ),
append: false
file name: 'stacktraceAppender',
file: "${logDirectory}/${myAppName}_stacktrace.log",
threshold: org.apache.log4j.Level.ERROR,
append: false
} // appenders
root {
warn 'fileAppender', 'stdoutAppender'
}
error stacktraceAppender: "StackTrace"
error fileAppender: [
'grails.app',
'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'
]
environments {
development {
debug additivity: false,
stdoutAppender: [
"grails.app.conf.BootStrap"
]
debug additivity: false,
fileAppender: [
"grails.app.conf.BootStrap"
]
} // development
production {
info additivity: false,
fileAppender: [
"grails.app.conf.BootStrap"
]
} // production
} // environments
}
// ----------------------------- End Config.groovy snippet