Discussion:
Logging to two locations in Grails 2.3.8
Ken Tanaka - NOAA Affiliate
2014-05-19 21:37:48 UTC
Permalink
Hi,

I'd like to log some information from my BootStrap.groovy file to report
some configuration information and mark the transition from configuration
to execution phase of the application. This should appear in both the
logfile and on the console when I'm using development mode. My environment
consists of

Grails 2.3.8
Red Hat Enterprise Linux Workstation release 6.5

The trouble I'm having is seeing the BootStrap log.info output appear only
on the console when I run "grails run-app". 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). If you have any suggestions on my log4j
configuration I'd appreciate it a lot.

-Ken

I have modified my BootStrap.groovy:

// ----------------------------- Start 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 = {
}
}
// ----------------------------- End BootStrap.groovy

In Config.groovy the log4j configuration:

// ----------------------------- 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
Silvio Wangler
2014-05-20 05:11:54 UTC
Permalink
Please see http://grails.1312388.n4.nabble.com/Deprecating
-The-Grails-Mailing-Lists-td4656735.html.

Thanks for your cooperation.
Post by Ken Tanaka - NOAA Affiliate
Hi,
I'd like to log some information from my BootStrap.groovy file to report
some configuration information and mark the transition from configuration
to execution phase of the application. This should appear in both the
logfile and on the console when I'm using development mode. My environment
consists of
Grails 2.3.8
Red Hat Enterprise Linux Workstation release 6.5
The trouble I'm having is seeing the BootStrap log.info output appear
only on the console when I run "grails run-app". 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). If you have any suggestions on my log4j
configuration I'd appreciate it a lot.
-Ken
// ----------------------------- Start 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}
${grailsApplication.config?.dataSource?.username}
dataSource.url : ${grailsApplication.config?.dataSource?.url}
------------------------------------------------------}"""
}
def destroy = {
}
}
// ----------------------------- End BootStrap.groovy
// ----------------------------- 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
Loading...