Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Log4J trying to log into separate files for one controller only fails

I would like to log messages from one controller in my grails application at info level to a specific file. Everything else at info or any other level should go to another default logging file.

However, I cannot get log4J to populate the usage.log file at all, although it happily logs to the 'default' appender defined below.

Can anyone give any clues as to why my setup is not working?

My log4j setup is as follows:

log4j = {

appenders {

    console name: 'stdout', 
    layout: pattern(conversionPattern: '%d %-5p %c{1} - %m%n')

    appender new DailyRollingFileAppender(
            name: 'default', 
            datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: '%d %-5p %c{1} - %m%n'),
            file: 'C:/logs/default.log'
         )

    //daily usage log
    appender new DailyRollingFileAppender(
            name: 'usage', datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: '%d %-5p %c{1} - %m%n'),
            file: 'C:/logs/usage.log'
         )
}


info usage: "grails.app.controllers.com.example.MyController", 
additivity: false

root {
    info 'stdout', 'default'
    additivity = true
}

     info 'grails.app'
}

UPDATE

I have been able to solve this by replacing the specific controller class mapping with

info usage: 'usage', additivity: true

and then using

private static final log = LogFactory.getLog("usage")

No other documented or described way seems to work, and ideally I'd be able to configure it on a package or class basis, but this works.

like image 348
user1740752 Avatar asked Oct 12 '12 10:10

user1740752


1 Answers

You need to be careful with the named parameter types that info et. al. expects. The following configurations works in a Grails 2.1.1 test project:

// log4j configuration
log4j = {
    appenders {
        def logPattern = '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{2} - %m%n'
        console name: 'stdout', layout: pattern(conversionPattern: logPattern)

        appender new DailyRollingFileAppender(
            name: 'usage', datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: logPattern),
            file: 'usage.log'
        )
    }

    root {
        error 'stdout'
    }

    info additivity: false, usage: ["grails.app.controllers.com.example.MyController"]
} 
like image 115
Andre Steingress Avatar answered Sep 24 '22 00:09

Andre Steingress