BioBier
BioBier

Reputation: 84

Grails logging from controller

I'm having a hard time to get Grails 2.4.5 to log my custom messages from my controllers into a specific file. This is what I have in the config part:

log4j = {
    /*
     * Log levels
    off
    fatal
    error
    warn
    info
    debug
    trace
    all 
     */
    appenders {
        console name:'stdout',  threshold: org.apache.log4j.Level.INFO
        rollingFile name: 'applog', maxFileSize: 1024, file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',
            layout: pattern(conversionPattern: "[%d{HH:mm:ss:SSS}] %-5p %c{2}: %m%n")
        environments {
            development {
                rollingFile name: 'grailsfile', maxFileSize: 4096, file: 'target/logs/grails.log'
                file name: 'rootlog', file: 'target/logs/root.log'              
            }
        }
    }
    root { error 'stdout'}
    environments {
        development {
            debug additivity: false, 'grails.app'
            root { error 'stdout', 'rootlog'} //override the root
            warn additivity: false, grailsfile: 
                ['org.codehaus.groovy.grails.web.servlet',        // controllers
                 'org.codehaus.groovy.grails.web.pages',          // GSP
                 'org.codehaus.groovy.grails.web.sitemesh',       // layouts
                 'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
                 'org.codehaus.groovy.grails.web.mapping',        // URL mapping
                 'org.codehaus.groovy.grails.commons',            // core / classloading
                 'org.codehaus.groovy.grails.plugins',            // plugins
                 'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
                 'org.springframework',
                 'org.hibernate',
                 'net.sf.ehcache.hibernate']
            debug additivity: false,  applog: 
            ['grails.app.controllers', 'grails.app.domain', 'grails.app.services']      
        }
//      production {
//          root {  error() }
//          warn "grails.app.controllers"
//          warn "grails.app.domain"
//          warn "grails.app.services"
//          warn additivity: false,  applog: 'grails.app'
//      }
    }
}

in the controller:

println "Debug log enabled?: " + log.debugEnabled   
log.debug "First piggy wrote to Debug"
log.info "Second piggy wrote to Info"
log.error "And the third piggy wrote to Error"

That shows me "Debug log enabled?: true" on console. But the message are not logged anywhere. The Grails internals log to "grailsfile" and also errors go to "rootlog". But appName.log stays empty. Any hint what I am having wrong?

UPDATE

To simplify my request I have only configured the most important now. I want the lines

log.debug "First piggy wrote to Debug"
log.info "Second piggy wrote to Info"
log.error "And the third piggy wrote to Error"

End up in the appender applog (file) So the log4j now only has:

log4j = {
    appenders {
        console name:'stdout'//,  threshold: org.apache.log4j.Level.INFO
        rollingFile name: 'applog', maxFileSize: 1024, file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',
            layout: pattern(conversionPattern: "[%d{HH:mm:ss:SSS}] %-5p %c{2}: %m%n")
    }
    environments {
        development {
            debug 'grails.app'
            debug additivity: false
                applog: ['grails.app.controllers', 'grails.app.domain', 'grails.app.services']      
        }
    }
}

But Still I get my log lines (and also errors from other sources) into console while my custom log file stays empty.

So I simply want to get (ONLY) my log lines into the applog appender as a first step. Any hint how to achieve this?

Upvotes: 0

Views: 3361

Answers (2)

BioBier
BioBier

Reputation: 84

meanwhile I could figure out that this property made the trouble for my custom log messages:

def appName = grails.util.Metadata.current.'app.name'
...
file: (System.getProperty('catalina.base') ?: 'target') + '/logs/' + appName + '.log',

changing to this works:

log4jFileName = (System.properties.'catalina.base') + "/logs/${appName}.log"
...

file:  "${config.log4jFileName}",

Upvotes: 0

Taras Kohut
Taras Kohut

Reputation: 2555

If you are running the app in development mode, then all your logs with error level and higher are going to target/logs/root.log It is configured here:
file name: 'rootlog', file: 'target/logs/root.log' ...
root { error 'stdout', 'rootlog'} //override the root

That's because development closure overrides your global configuration. If you want to see all the logs in appName.log, you should set lower logging level and change appender like this:
root { info 'stdout', 'applog'} //override the root

Upvotes: 1

Related Questions