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

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.

Answers


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.


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 {

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()

Log4j is one of the best logging for groovy

import groovy.util.logging.Log4j

@Log4j
public class MyClass{
//Use for logger check
public static void myMethod(){
    //log.error(null, "This is the log message", throwable)

    //log.error(null, "This is the log message", throwable)

    //log.info("This is the message for info")

    //log.debugg("This is the message for debugging")
}
}

Log4j.properties

# Define the root logger with appender file
log =folderpath
log4j.rootLogger=INFO, R, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%C:%L [%t]  - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${log}/filename.log

log4j.appender.R.MaxFileSize=2048KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern="%d %5p %c{1}:%L - %m%n"
# %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%C:%L [%t]  - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Hope it will help you....


Well I have done this in grails 3.1.8 which uses Logback.

import org.apache.commons.logging.LogFactory

public class MyClass{

   static final LOG = LogFactory.getLog(this)

   def function(){
     LOG.debug "Debug message"
   }

   static staticFunction(){
     LOG.debug "Another debug message"
   }
}

Need Your Help