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...
}
}
}
Related
Using legacy grails 2.2
In the config environments I have set the debug to be app wide
test{
log4j={
root{
error 'stdout'
debug 'stdout'
}
debug 'grails.app'
}
}
I have the parameter
-echoOut
in the command line options.
But if I run a test case with a log.debug somewhere in the chain, I get nothing in the console or in the generated report. What am I missing?
You can solve this by adding your apps package name to debug 'grails.app' like debug 'stdout': ['grails.app', 'your.package']
I want to use ServerURL in config.groovy.
But, I need configure url.setting both development and production if I cannot use serverURL in config.groovy.
I try to get serverURL like below, it makes error.
config.groovy
def grailsApplication
environments {
development {
grails.logging.jul.usebridge = true
grails.serverURL = "http://localhost:8080"
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://myapp.com"
}
}
url.setting = "${grailsApplication.config.grails.serverURL}"
the error when I run app is like below
Error Error packaging application: Error loading Config.groovy: Cannot get property 'config' on null object (Use --stacktrace to see the full trace)
You can get the server URL like-
url.setting="${grails.serverURL}"
In my Grails project I've created and implemented log4j logging system.
In local environment file is correctly created, but in production server it is not.
here is the log4j configuration inside config.groovy file:
log4j = {
appenders {
file name: 'file', file:"/home/file.log"
}
root{
info 'stdout', 'file'
}
}
I've tried with other paths but it does not work.
How can I solve this issue?
I have been able externalize DataSource configurations for development environment by updating grails.config.locations in Config.groovy and then setting specific configurations in .grails/${appName}-config.properties as following:
dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url = jdbc:mysql://localhost/db-dev
dataSource.username = root
dataSource.password = pass
For the 'test' environment, i have different Database so i would like to updated test.dataSource.url accordingly but this doesn't work. It still takes the 'dev' dataSource.url when running 'grails test' mode(i.e. grails test dgm-update). How to externalize test environment DataSource configuration?
Thank You for helping
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-${grails.util.Environment.current.name}.properties"]
and then put nested files for each env:
/home/me/.grails/myapp-config-dev.properties
/home/me/.grails/myapp-config-test.properties
/home/me/.grails/myapp-config-production.properties
Try to use Groovy external configuration, i.e. ".grails/${appName}-config.groovy" with environment specific DSL:
environments {
test {
dataSource {
...
}
}
}
This seems pretty strange, but when grails builds a war file it doesn't generate a log4j.properties or log4j.xml file.
Instead it has the following in WEB-INF/web.xml
web.xml:
<listener>
<listener-class>org.codehaus.groovy.grails.web.util.Log4jConfigListener</listener-class>
</listener>
and apparently "grails Log4j DSL configures logging in-memory". The problem here is - log4j isn't automatically exposed to JMX for us to dynamically change and there's no log4j file generated by grails. But Config.groovy is a compiled file.
There's got to be an easy way to manage this without rebuilding the war?
One option suggested is go through to spring and configure logging there:
resources.groovy:
beans = {
log4jConfigurer(org.springframework.beans.factory.config.MethodInvokingFactoryBean)
{
targetClass = "org.springframework.util.Log4jConfigurer"
targetMethod = "initLogging"
arguments = ["classpath:myapp/log4j.properties"]
}
}
then shift the configuration in the DSL to the configured file.
Can anyone advise the 'groovy' way to dynamically change logging configuration without rebuilding the WAR file each time. Using grails-1.3.7. Cutting the DSL out doesn't seem the right way.
Thanks
You may have an external config file that is searched for by your application at startup time.
You would have a MyExternalConfig.groovy file somewhere in your production environment. For example:
log4j = {
def catalinaBase = System.properties.getProperty('catalina.base')
if (!catalinaBase) catalinaBase = '.'
def logDirectory = "${catalinaBase}/logs"
appenders {
rollingFile name:"infoLog", maxFileSize:'900KB', file:"${logDirectory}/${appName}Info.log", maxBackupIndex:10, layout:pattern(conversionPattern: '%d{DATE} %p %c - %m%n'), threshold: org.apache.log4j.Level.INFO
rollingFile name:"erroLog", maxFileSize:'900KB', file:"${logDirectory}/${appName}Erro.log", maxBackupIndex:10, layout:pattern(conversionPattern: '%d{DATE} %p %c - %m%n'), threshold: org.apache.log4j.Level.ERROR
}
root {
info 'infoLog', 'erroLog'
additivity = false
}
error erroLog:"StackTrace"
error erroLog: 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'net.sf.ehcache.hibernate'
warn infoLog: 'org.mortbay.log'
info infoLog: "grails.app"
}
Then in your Config.groovy file, that belongs to your grails project in conf folder, you put this as the last thing of the file:
def ENV_NAME = "MY_EXTERNAL_CONFIG"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
grails.config.locations = []
}
if(System.getenv(ENV_NAME)) {
grails.config.locations << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
grails.config.locations << "file:" + System.getProperty(ENV_NAME)
} else {
println "No external configuration file defined."
}
This will look for external configurations files to add to your grails.config.locations attribute of your Config.groovy. First it looks for it as a System Environment variable (I use it this way), if it does not find, then it looks for a command line parameter (so you could add it when you start your tomcat app, as a parameter to startup.sh).
To configure your system environment variabble, just do this before starting tomcat:
MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyExternalConfig.groovy"
export MY_EXTERNAL_CONFIG
--- start tomcat here ---
That's it.