log4j : parameter appender file name - grails

I use Grails
In my file Config.groovy I create an appender as:
log4j = {
appenders {
file name:'myAppli', file:'/tmp/myAppli.log'
}
...
}
Is it possible to parameter file path of my appender through data of file.properties ?
something like that :
file.properties:
myAppli.log.path=C:\\tmp\\
Config.groovy:
appenders {
file name:'myLogs', file:myAppli.log.path + 'myLogs.log'
}

myAppli.log.path should work!!!

There's a section in the docs for this: externalized configuration. You can set a absolute location or let Grails look into the classpath. Here's the example of the docs:
grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy" ]
EDIT: I tested here. It appears that the value is only available throught the config object during runtime and not available inside Config.groovy. According to this thread it's not possible to do what you want.

You're almost right. The log4j closure is executed after the whole configuration has been parsed and assembled, and within the closure you have access to the complete configuration via the variable config. You can say
grails.config.locations = ['file:file.properties']
log4j = {
appenders {
file name:'myAppli', file:"${config.myAppli.log.path}myLogs.log"
}
// ...
}
I've tested this with Grails 2.2: run grails create-app log4jtest to create a new application, then edit log4jtest/grails-app/conf/Config.groovy to add at the top
grails.config.locations = ["file:file.properties"]
logfile.name = "from-config.log"
and for the log4j closure
// log4j configuration
log4j = {
println "filename: ${config.logfile.name}"
// rest of closure as before
Run this app using grails run-app and you'll see it print filename: from-config.log (twice, in fact). Now create a file named file.properties in the top-level log4jtest folder containing the line
logfile.name=from-external.log
Run the app again and this time it will print filename: from-external.log instead.

Related

Grails configuration ConfigSlurper

I want to separate config files into few small parts. In Config.groovy I have defined grails.config.locations array to point these files:
grails.config.locations = [
"classpath:config.properties",
"classpath:some-config.groovy",
]
And then I am checking configuration map by accessing: grailsApplication.config
The first configuration file is Java properties file, which loads properly:
config.properties
grails.serverURL=http://localhost:8080/selly
..
The second one is .groovy file which in reference to the documentation (http://grails.org/doc/latest/guide/conf.html#configExternalized) should be loaded from automatically parsed ConfigSlurper file format:
some-config.groovy:
app {
testvar {
foo = true
}
}
But grailsApplication.config.app does not exists (no field in debug and println returns empty map [:]).
Can anyone give an example of loading groovy files?
Files are placed in: grails-app\conf\, for example grails-app\conf\config.properties
It looks like you've configured both files correctly. grailsApplication.config.app might be null simply because it is not a leaf node, have you tried grailsApplication.config.app.testvar.foo?

Grails not reading plugin's Config.groovy file

I have a Grails (2.3.6) app (myapp) and a custom plugin (grails-myapplogging). I want to:
Configure log4j in the plugin and nowhere else
In keeping with the above, this implies that I am omitting any log4j closures inside the app's Config.groovy file
Per this answer, I added a Config.groovy file to my plugin and gave it this:
println("Well hello!")
log4j {
appenders {
appender new org.apache.log4j.DailyRollingFileAppender(
name: "appLog",
datePattern: "'.'yyyy-MM-dd",
fileName: "/var/log/myapp.log"
)
}
root {
error 'stdout', 'appLog'
}
}
Then in my app's BuildConfig.groovy, I add the plugin to my project. Then I run the app (grails run-app) and expect to see some logging. The app starts up just fine, doesn't throw or print any errors, however /var/log/myapp.log never gets created. Additionally, when building, I never see the "Well hello!" message get printed out. Ideas?
When I take this log4j closure and put it into my app's Config.groovy, everything works perfectly fine. So this is definitely a situation where the plugin's Config.groovy is not being read.
Update: do I need to manually "include" the plugin's Config.groovy somehow, or explicitly tell the app to load it, etc.?

Can I split config.groovy file of grails?

As there is sensitive code in the config.groovy file, I am afraid that my friends will commit with bugs in this file. When getting svn update, we too will get the buggy config code.
Can i split the code at config.groovy in such a way that the sensitive code remains untouched and the other can be changed frequently?
Inside your main config file you are able to access this variable:
grails.config.locations
It is a list of configuration file locations to which you can add your own files:
grails.config.locations << 'file:MyConfigFile.groovy'
These files will then be added to your configuration.
For a more elaborate setup see this blog post:
http://www.pasopas.nl/2012/loading-grails-configuration-files-update/
Similar to Marijn's answer. This is how I usually set up my Config.groovy. I still use it for some settings, but anything environmentally (deployment location or individual machine) changing can override any settings in the Config.groovy.
Config.groovy >>>>
grails.config.locations = [
"file:../app-config/myapp-dataSource.groovy",
"file:../app-config/myapp-config.groovy"
]
environments {
development {
grails.config.locations = [
"file:../myapp-config/myapp-dataSource.groovy",
"file:../myapp-config/myapp-config.groovy",
"file:${userHome}/myapp-config/myapp-dataSource.groovy",
"file:${userHome}/myapp-config/myapp-config.groovy"
]
some.config.setting=true
}
}
file:${userHome}/myapp-config/myapp-config.groovy >>>>
some.config.setting=false

Grails External Config Read Incorrectly on First Load

Grails 1.3.7
I have some configuration located in an external config file. One of the entires looks like this:
site.maintenance.mode = false
I have a filter which checks for certain config settings for specific URLs. When I do a run-app or deploy a WAR into Tomcat and do:
boolean maintenanceMode = grailsApplication.config.site.maintenance.mode
maintenanceMode is coming back true. If I look at the config object in debug mode, this is what I get:
site={maintenance={mode=false, message="<p>Our trail guides are working hard to get the system back on track.</p><p>We're sorry, the account system is down for maintenance at the moment. We'll get it back online as quickly as we can. Thanks for your patience.</p>"}}
I have a controller that I use to reload this config file dynamically and hitting this controller will fix the issue. But I'm curious as to why it is incorrect on first runs and why the discrepency in what is getting put in the maintenanceMode variable vs what is actually in the config object.
Are you using a Java properties file or a Groovy file? If you're using a properties file then I believe Grails will interpret site.maintenance.mode=false the same way as site.maintenance.mode='false' and since Groovy will interpret:
"false".asBoolean() == true
then that would explain why you would see that initial true value.
I just ran a simple test locally to verify this behavior. When I externalize my properties in a file called test.properties then site.maintenance.mode=false initially gets a boolean value of true, when I use a file called test.groovy then it interprets the boolean value of site.maintenance.mode=false as false. I believe this is because when you use a Groovy file Grails uses ConfigurationSlurper to process it but when you use a properties file Grails interprets everything as String name/value pairs.
What I do is to have an external Config.groovy file, for instance: MyConfig.groovy
At the end of the standard grails Config.groovy file, I have the following:
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 Configs found."
}
So now you can have a MyConfig.groovy file anywhere in production environment (for example) and then set an Environment system variable to point to this file (or pass it as parameter to startup.sh), before you start tomcat:
MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyConfig.groovy"
export MY_EXTERNAL_CONFIG
That's it. Now you have an external MyConfig.groovy file. The properties in it are accessible from your grails app as they were part of the standard Config.groovy
import org.codehaus.groovy.grails.commons.*
//...
ConfigurationHolder.config.foo.bar.hello

Using External Configuration File

I've added the code below to my Config.groovy file, however, in spite of having it I'm not able to access the external configuration properties in the Config.groovy file.
Does anyone how I can access the properties of the external configuration file within the Config.groovy file?
if (System.getProperty("CONFIG")) {
grails.config.locations << "file:" + System.getProperty("CONFIG")
} else {
grails.config.locations << "file:./${appName}-config.properties"
}
Note: I've tried using ${...} like I would in Spring configuration files, ConfigurationHolder.config, and grailsApplication to access the properties but none of these approaches work.
Thanks.
I think this would cause confusion due to the order the config files are loaded. I'm pretty sure at the time the Config.groovy is loaded, the external one hasn't been loaded yet.
So your "CONFIG" property is set in the external file, which is the name of the file that you want to load?
How I usually do this is just list all the files that I could use.
grails.config.locations = [
"file:../app-config/myapp-dataSource.groovy",
"file:../app-config/myapp-config.groovy"
]
environments {
development {
grails.config.locations = [
"file:../myapp-config/myapp-dataSource.groovy",
"file:../myapp-config/myapp-config.groovy",
"file:${userHome}/myapp-config/myapp-dataSource.groovy",
"file:${userHome}/myapp-config/myapp-config.groovy"
]
}
...
}
If the files do not exist they are just skipped. Files I believe are loaded in order, so anything in the ${userHome} dir, will override the previously set values. This is nice for development, as you can have machine local ways of changing settings, and not have to worry about these config changes being checked in.
#Nick Larson,
What you mentioned about CONFIG not loaded is not true. If CONFIG is a JVM parameter, set with -DCONFIG=xxxx, then it is set before config.groovy kicks in.
#Kin1,
You are using file: protocol for accessing the property file. Are you trying to access this in a WAR or EAR file or is it a file based system.
In a WAR or EAR file you need to use classpath: for the file, file: does not work. Also, you have to make sure to actually copy the Groovy file(not compiled class file) in the classpath. We do it on WAR create event and the build process copy the config file into one of the classpath location.
Hope this helps.
Add the below line in config.groovy
grails.config.locations = [ "classpath:grails-app-config.properties"]
environments {
development {
grails.logging.jul.usebridge = true
grails.config.locations = ["file:C:\\conf\\externalfile.groovy"]
}
production {
grails.logging.jul.usebridge = false
grails.config.locations = ["file:/opt/config/externalfile.groovy"]
// TODO: grails.serverURL = "http://www.changeme.com"
}
}
If you want to access any property from external configuration(config.groovy) then just declare the property like
property = property value eg:(ImagePath = "C:\\Users\\Saved Pictures")
access it like grailsApplication.config."property"
eg:(grailsApplication.config.ImagePath)
NOTE: dont use def just a property and its value.

Resources