tags:

views:

134

answers:

1

I'm encountering this error:

groovy.lang.MissingPropertyException: No such property: log for class: org.utils.MyClass

Here's the content of the class:

package org.utils

class MyClass {
    int organizationCount = 0

    public int getOrganizationCount(){
        log.debug "There are ${organizationCount} organization(s) found."
        return organizationCount
    }

}

Do i need to add an import statement? What do i need to add? Note that the class is located in src/groovy/org/utils. I know that the 'log' variable is accessible in controllers, services, etc. Not sure in 'src' classes.

Thanks.

+2  A: 

The log variable is injected by grails and thus only available in the grails-specific classes like controllers, services, etc. - and I don't think you can "import" that in any way.

Outside these classes, you'll just have to use log4j "regularly", i.e.

Logger.getLogger(MyClass.class).debug()
Michael Borgwardt
thanks for the lead! unfortunately, i'm having issue on using 'debug' method. i'm having this exception:Error: No signature of method: java.util.logging.Logger.debug() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [There are 15363 organization(s) found.]however, when i tried 'info' level, everything's find. Here's the sample code:String s = "There are ${organizationCount} organization(s) found."Logger.getLogger(this.class.getName()).info(s)Note that when i replace 'info' with 'debug', i'm having that exception.
firnnauriel
@firnnauriel: looks like you're running into a problem with the overloaded debug method vs. groovy's type magic. Try building the string as a "traditional" Java string using +, instead of a GString.
Michael Borgwardt
You could also use Log4j, which will call toString() on the argument, so it doesn't matter if it's a GString or String. Just import org.apache.log4j.Logger instead of the JUL Logger.
Burt Beckwith