将外部配置值导入grails中的主配置

时间:2013-11-28 00:15:16

标签: grails configuration

我有一个外部配置文件 $ {userHome} / .grails / $ {appName} /config.groovy

package configs

grails.conf.logDirectory = '/home/serek/tmp/mamlog'

我想导入主要Grails Config.groovy

grails.config.locations = ["file:${userHome}/.grails/${appName}/config.groovy"]
print grails.conf.logDirectory
log4j = {

    appenders {
        rollingFile name: 'infoLog', file: "${grails.conf.logDirectory}/info.log", threshold: org.apache.log4j.Level.INFO, maxFileSize: "1024MB", append: true
        rollingFile name: 'warnLog', file: 'warn.log', threshold: org.apache.log4j.Level.WARN, maxFileSize: "1024MB", append: true
        console name: 'stdout', layout: pattern(conversionPattern: '%d{yyyyMMdd.HHmmss.SSS} %r [%t] %-5p %c %x - %m%n')
    }

不幸的是, print grails.conf.logDirectory 在主配置中可见。

我该怎么处理?仅打印输出[:]

Groovy: 2.1.9  
Grails: 2.3.2

========================================
我找到了解决方案,来自Config.groovy:

import org.yaml.snakeyaml.Yaml

    Yaml yaml = new Yaml()
    def extConfFilePath = "${userHome}/.grails/${appName}/mam.yaml" //my external conf in yaml
    def extConfFileContent = new File(extConfFilePath).text
    def extConf = yaml.load(extConfFileContent)
    grails.ext = extConf


    rollingFile name: 'infoLog', file: extConf.logDirectory + "info.log", threshold: org.apache.log4j.Level.INFO, maxFileSize: "1024MB", append: true

1 个答案:

答案 0 :(得分:1)

我相信它是因为外部配置文件在执行 Config.groovy之后被拉入。因此,该位置的println不起作用。尝试使用

从任何gsp页面打印它
println "logDir = ${grailsApplication.config.grails.conf.logDirectory}"