Log4j in grails: error printed when using variable in appender configuration

I am using slf4j "DailyRollingFileAppender" in grails 2.3.4.

When I tried to use a variable as part of the file parameter, grails always prints some error logs when the application starts.

But my application log messages may print in the specified "user-event.log" as expected, although grails gives me these 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 variable "event.log.dir" is defined 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"
    }
}

Error messages printed in the grails console when starting the application:

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

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

+2
1

Holders . , config log4j, .

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"
}
+5

Source: https://habr.com/ru/post/1538999/


All Articles