Why can't I get my logger to write to the file appender in Grails?

906 Views Asked by At

I have a controller and a service named ApiController and ApiService.groovy. I have my Config.groovy file setup to write the error and info logs to a file using an appender. When I call log.info("foo") in my service, it only writes to the STDOUT and not to the file. The files are created, but never written to.

Here's my Config.groovy

// log4j configuration
def isProd = Environment.current == Environment.PRODUCTION

def catalinaBase = System.properties.getProperty('catalina.base')
if (!catalinaBase) catalinaBase = '.'   // just in case

def logDirectory = "${catalinaBase}/logs"
if (!isProd) {
    logDirectory = 'logs'
}

log4j = {
    // Example of changing the log pattern for the default console
    // appender:
    //
    appenders {
        appenders {
            console name: 'debug,info,warn,error,stdout,logfile', layout: pattern(conversionPattern: '%c{2} %m%n')
            appender new RollingFileAppender(name: "apiLog", maxFileSize: 1024, file: "${logDirectory}/api.log")
            appender new RollingFileAppender(name: "pingDupLog", maxFileSize: 1024, file: "${logDirectory}/pingDup.log")
            appender new RollingFileAppender(name: "fraudLog", maxFileSize: 1024, file: "${logDirectory}/fraud.log")
            appender new RollingFileAppender(name: "appLog", maxFileSize: 1024, file: "${logDirectory}/app.log")
        }
    }

    error   apiLog: ['grails.app.services.com.ono.catfish.ApiService',
                     'grails.app.controllers.com.ono.catfish.ApiController'],

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

    info    apiLog: ['grails.app.services.com.ono.catfish.ApiService',
                     'grails.app.controllers.com.ono.catfish.ApiController']

    root {
        info 'stdout', 'appLog'
    }
}

And here's my service grails-app/com/ono/catfish/ApiService.groovy:

package com.ono.catfish

class ApiService {

    def ping(Map params) {

        log.info("Received submission: ${params.toMapString()}")
        log.error("Testing Error Log")

    }
}

Can anyone help me figure out what I'm doing wrong?

Thanks!

1

There are 1 best solutions below

0
On

My guess is that you should apiLog appender to your root logger like this;

root {
    info 'stdout', 'appLog', 'apiLog'
}

Also your console appender name is wrong, it should be "stdout" only.