Some questions about Grails logging and external c

2019-08-26 19:33发布

问题:

I have 2 problems about Grails logging and using external config file.
1. In src/groovy folder, I put a class A and I want to log events in some methods, but I can not use the built-in "logger", because this class is outside from service and controller scope of Grails application. I try to use LogFactory.getLog("A.class"), but I do not see anything write out from this logging variable. So how can I force this additional logger to write log in to the same log file which is created by default? Or is it possible to re-use the built-in logger provided by Grails?
2. I want to allow user to re-define some parameters which will be used in application, so I create an external configuration file and build the syntax of file content like the way Grails apply to messages resource files (code=value). So can I make Grails to understand and deal with that file as other message resource files? And how to include it to the resource classpath of application?
Thank you so much!

回答1:

Here's how to add a configuration file to a Grails project.

Create a properties file myExternalProperties.groovy and put it on your classpath (such as the $TOMCAT_HOME/lib directory).

Create a configuration file grails-app/conf/MyConfig.groovy to use the external configuration values (if needed). You will not be able to use the properties defined in myExternalProperties.groovy within grails-app/conf/Config.groovy.

Edit grails-app/conf/Config.groovy. Uncomment the lines the define grails.config.locations and add this:

grails.config.locations << "classpath:MyExternalProperties.groovy"
grails.config.locations << "classpath:MyConfig.groovy"

Add the following to scripts/Events.groovy (which probably needs to be created).

 eventCompileEnd = {
     ant.copy(todir:classesDirPath) {
         fileset(file:"${basedir}/grails-app/conf/MyConfig.groovy")
     }
 }

That last part is very important.