开发者

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?


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'
    }
  }
}


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...
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜