Ace.Yin
Ace.Yin

Reputation: 917

log4j in grails: error printed when use variable in appender config

I am using slf4j "DailyRollingFileAppender" in grails 2.3.4.

When i tried to use a variable as part of the "file" parameter, the grails always print some error logs when the app starting.

But my app's log messages can printed into the specified "user-event.log" as expected although grails gives me those error messages.

Below is my log4j configuration:

log4j = {
    // Example of changing the log pattern for the default console appender:
    appenders {
        console name: 'stdout', layout: pattern(conversionPattern: '%c{2} %m%n')
        appender new DailyRollingFileAppender(
                name: "userEventLog",
                file: "${event.log.dir}/user-event.log",
                layout: pattern(conversionPattern: '%m%n'),
                datePattern: "'.'yyyy_MM_dd",
                threshold: org.apache.log4j.Level.INFO
        )
    }

    info userEventLog: "app.bean.log.UserEventLog"
}

The "event.log.dir" variable is defined as below :

environments {
    development {
        // event log dir
        event.log.dir = "${userHome}/workspace/app/logs/event"
    }
    production {
        // event log dir
        event.log.dir = "/opt/www/app/logs/event"
    }
}

The error messages printed in grails console when app starting are:

| Error log4j:ERROR Property missing when configuring log4j: event
| Error log4j:ERROR Property missing when configuring log4j: event

when i replace the "${event.log.dir}" variable with a string path, for example "/home/app/" and restart grails, the error messages disappear.

Upvotes: 2

Views: 2078

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

Use Holders to read config properties. I assume it will not be able to read the config property itself while setting up log4j as it is part of the config itself.

import grails.util.Holders

...

log4j = {
// Example of changing the log pattern for the default console appender:
    appenders {
        console name: 'stdout', layout: pattern(conversionPattern: '%c{2} %m%n')
        appender new DailyRollingFileAppender(
            name: "userEventLog",
            file: "${Holders.config.event.log.dir}/user-event.log",
            layout: pattern(conversionPattern: '%m%n'),
            datePattern: "'.'yyyy_MM_dd",
            threshold: org.apache.log4j.Level.INFO
        )
    }

    info userEventLog: "app.bean.log.UserEventLog"
}

Upvotes: 5

Related Questions