关于Grails日志记录和外部配置文件的一些问题 [英] Some questions about Grails logging and external config file

查看:98
本文介绍了关于Grails日志记录和外部配置文件的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个关于Grails日志记录和使用外部配置文件的问题。

1.在src / groovy文件夹中,我放置了一个类A,并且我想用某些方法记录事件,但是我无法使用内置的记录器,因为这个类不在Grails应用程序的服务和控制器范围之内。我尝试使用LogFactory.getLog(A.class),但是我没有看到从此日志记录变量中写出任何内容。那么我怎么能强制这个额外的记录器写入登录到默认创建的同一个日志文件呢?或者有可能重新使用Grails提供的内置记录器?

2.我想让用户重新定义一些将在应用程序中使用的参数,所以我创建了一个外部配置文件并像Grails应用于消息资源文件(代码=值)一样构建文件内容的语法。那么,我可以让Grails了解并处理该文件作为其他消息资源文件吗?如何将它包含到应用程序的资源类路径中?

非常感谢!

解决方案

如何将配置文件添加到Grails项目。

创建一个属性文件 myExternalProperties.groovy 并将其放在你的类路径中(比如 $ TOMCAT_HOME / lib中目录中)。



创建配置文件的grails-app / CONF / MyConfig.groovy 来使用外部配置值(如果需要)。你将不能够在的grails-app / CONF / Config.groovy中



使用myExternalProperties.groovy定义的属性

编辑 grails-app / conf / Config.groovy 。取消注释定义grails.config.locations的行并添加以下内容:

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

将以下内容添加到 scripts / Events中。 groovy (可能需要创建)。

  eventCompileEnd = {
ant。拷贝(todir:classesDirPath){
文件集(文件: $ {BASEDIR} /grails-app/conf/MyConfig.groovy)
}
}

最后一部分非常重要。

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!

解决方案

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.

这篇关于关于Grails日志记录和外部配置文件的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆