I'd like to log some information from my BootStrap.groovy file to the console and a logfile. The output reports some configuration information and marks the transition from configuration to the execution phase of the application. The problem I have is seeing the desired output only on the console, but not in the log file. My environment consists of
- Grails 2.3.8
- Red Hat Enterprise Linux Workstation release 6.5
I've provided two code snippets to show what I'm doing. They would also be easy to plug into an empty Grails project (no domain classes or controllers need be created to see the behavior I'm seeing). When I run a 'grails run-app' I expect the development section of the environments
block to configure BootStrap output to two locations. Besides getting the logging to both locations working, if you have any general suggestions on my log4j configuration that would be appreciated also.
I have modified my 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 = {
}
}
In Config.groovy, the log4j configuration section is:
// ----------------------------- 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