Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use 'log' inside a src/groovy/ class

Tags:

grails

log4j

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.

like image 970
firnnauriel Avatar asked Apr 05 '10 08:04

firnnauriel


2 Answers

In grails 3, the default logging system is logback. Simply adding the @Slf4j annotation to your src/groovy class will take care of things.

import groovy.util.logging.Slf4j

@Slf4j
class MyUtil {
like image 186
Ryan Heathcote Avatar answered Nov 13 '22 20:11

Ryan Heathcote


In Groovy 1.8, you may also annotate the class with @Log (for java.util.logging) or @Log4j (for log4j) and it will "magically" have a log property. See http://docs.codehaus.org/display/GROOVY/Groovy+1.8+release+notes#Groovy1.8releasenotes-@Log for details.

PS.: If you use java.util.logging the log.debugcall will still fail because there's no debug method.

like image 7
Daniel Serodio Avatar answered Nov 13 '22 21:11

Daniel Serodio