The Fat Oracle
The Fat Oracle

Reputation: 646

Grails: Is there a debug flag I can check?

I have a few log.debugs() that I don't want to process (since they are heavy) unless the app is currently in debug mode (not production).

Is there a way to check if the grails app is currently in debug mode/development mode?

Upvotes: 3

Views: 1885

Answers (2)

Dónal
Dónal

Reputation: 187529

You can test if the current environment is dev (for example) using the following:

import grails.util.Environment

if (Environment.current == Environment.DEVELOPMENT ) {
    // Do your dev logging here
}

IMO, a better solution than hard-coding the env where this logging happens, is to configure it. For example, to enable debug logging for this class only in the dev environment. add the following to Config.groovy

log4j = {

  appenders {
    // config for stdout and logfile appenders omitted
  }

  // log everything at error level to stdout and logfile appenders
  root {
    error 'stdout', 'logfile'
  }

  environments {
    development {
      // log this class at debug level in dev env only
      debug 'com.example.MyClass'
    }
  }
}

Upvotes: 11

Aaron Saunders
Aaron Saunders

Reputation: 33345

in the config.groovy the environments are defined.

you can specify what you want the log configuration to be based on the environment the application in running in

environments {
    development {
        log4j = {
          // determine what appenders are logging in development...
        }
    }
    production {
        log4j = {
          // determine what appenders are logging in production...
        }
    }
}

Upvotes: 0

Related Questions