Valeriane
Valeriane

Reputation: 954

log4j : parameter appender file name

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'
}

Upvotes: 0

Views: 1270

Answers (3)

user1785360
user1785360

Reputation:

myAppli.log.path should work!!!

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122364

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.

Upvotes: 0

user800014
user800014

Reputation:

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.

Upvotes: 1

Related Questions