Logging to two locations in Grails 2.3.8

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Logging to two locations in Grails 2.3.8

Ken Tanaka
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
Reply | Threaded
Open this post in threaded view
|

Re: Logging to two locations in Grails 2.3.8

Silvio Wangler


On 19 May 2014 23:37, Ken Tanaka - NOAA Affiliate <[hidden email]> wrote:
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