【问题标题】:log4j : parameter appender file namelog4j : 参数附加文件名
【发布时间】:2013-01-25 15:23:29
【问题描述】:

我用Grails

在我的文件Config.groovy 中,我创建了一个附加程序:

log4j = {

    appenders {
        file name:'myAppli', file:'/tmp/myAppli.log'
    }
...
}

是否可以通过file.properties的数据来参数我的appender的文件路径?

类似的东西:

file.properties:
myAppli.log.path=C:\\tmp\\


Config.groovy:
appenders {
    file name:'myLogs', file:myAppli.log.path + 'myLogs.log'
}

【问题讨论】:

  • 您是否在 Config.groovy 中将 file.properties 添加到 grails.config.locations
  • 不,在 Config.groovy 中添加 file.properties 的语法是什么?

标签: grails groovy log4j grails-2.0


【解决方案1】:

myAppli.log.path 应该可以工作!!!

【讨论】:

    【解决方案2】:

    文档中有一个部分:externalized configuration。您可以设置绝对位置或让 Grails 查看类路径。这是文档的示例:

    grails.config.locations = [
        "classpath:${appName}-config.properties",
        "classpath:${appName}-config.groovy",
        "file:${userHome}/.grails/${appName}-config.properties",
        "file:${userHome}/.grails/${appName}-config.groovy" ]
    

    编辑:我在这里测试过。似乎该值仅在运行时通过配置对象可用,而在 Config.groovy 中不可用。根据this 线程,不可能做你想做的事。

    【讨论】:

    • 如何在 appender 中使用它?
    • 像示例一样使用“appName”:${myAppli.log.path}
    • 好的,当我打印 grails.config.locations 时,我得到了正确的结果:我的属性文件的路径。我可以从 grails.config.locations 获取我的属性吗? (${myAppli.log.path} 不起作用)
    • 不,打印 {appName.log.path} ==> Config$_run_closure3@1b1e865
    【解决方案3】:

    你几乎是对的。 log4j闭包在整个配置被解析和组装后执行,在闭包中你可以通过变量config访问完整的配置。你可以说

    grails.config.locations = ['file:file.properties']
    
    log4j = {
        appenders {
            file name:'myAppli', file:"${config.myAppli.log.path}myLogs.log"
        }
        // ...
    }
    

    我已经用 Grails 2.2 对此进行了测试:运行 grails create-app log4jtest 以创建一个新应用程序,然后编辑 log4jtest/grails-app/conf/Config.groovy 以在顶部添加

    grails.config.locations = ["file:file.properties"]
    logfile.name = "from-config.log"
    

    对于log4j 闭包

    // log4j configuration
    log4j = {
        println "filename: ${config.logfile.name}"
        // rest of closure as before
    

    使用grails run-app 运行这个应用程序,您会看到它打印filename: from-config.log(实际上是两次)。现在在包含该行的顶级 log4jtest 文件夹中创建一个名为 file.properties 的文件

    logfile.name=from-external.log
    

    再次运行该应用,这一次它将打印filename: from-external.log

    【讨论】:

    • grails.config.locations
    • @Valeri config 变量在 log4j 闭包内 肯定可以正常工作(尽管在 Config.groovy 的其他地方没有)。请注意,grails.config.locations 命名的外部配置文件 必须 具有以 .groovy (如果它们是 groovy 文件)或 .properties (如果它们是属性文件)结尾的名称 - @987654339 @ 将不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多