Grails: Is there a debug flag that I can check?

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

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

+3
source share
2 answers

You can check 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 env where this logging takes place is to configure it. For example, enable debug logging for this class only in the dev environment. add the following toConfig.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'
    }
  }
}
+11
source

config.groovy defines environments.

you can specify that you want the log configuration to be based on the environment in which the application runs in

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

Source: https://habr.com/ru/post/1764403/


All Articles