【问题标题】:How can i use 'log' inside a src/groovy/ class我如何在 src/groovy/ 类中使用“日志”
【发布时间】:2011-02-04 09:02:27
【问题描述】:

我遇到了这个错误:

groovy.lang.MissingPropertyException: 没有这样的属性:类日志: org.utils.MyClass

这是课程的内容:

package org.utils

class MyClass {
    int organizationCount = 0

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

}

我需要添加导入语句吗?我需要添加什么?请注意,该类位于 src/groovy/org/utils 中。我知道在控制器、服务等中可以访问“日志”变量。在“src”类中不确定。

谢谢。

【问题讨论】:

    标签: grails log4j


    【解决方案1】:

    在 grails 3 中,默认的日志系统是 logback。只需将@Slf4j 注释添加到您的 src/groovy 类即可。

    import groovy.util.logging.Slf4j
    
    @Slf4j
    class MyUtil {
    

    【讨论】:

      【解决方案2】:

      在 Groovy 1.8 中,您还可以使用 @Log(对于 java.util.logging)或 @Log4j(对于 log4j)注释该类,它会“神奇地”拥有一个 log 属性。详情请见http://docs.codehaus.org/display/GROOVY/Groovy+1.8+release+notes#Groovy1.8releasenotes-@Log

      PS.:如果您使用 java.util.logging,log.debugcall 仍然会失败,因为没有 debug 方法。

      【讨论】:

        【解决方案3】:

        log 变量是由 grails 注入的,因此只能在 grails 特定的类(如控制器、服务等)中使用 - 我认为您不能以任何方式“导入”它。

        在这些类之外,您只需要“定期”使用 log4j,即

        Logger.getLogger(MyClass.class).debug()
        

        【讨论】:

        • 感谢领导!不幸的是,我在使用“调试”方法时遇到了问题。我遇到了这个异常:错误:没有方法签名:java.util.logging.Logger.debug() 适用于参数类型:(org.codehaus.groovy.runtime.GStringImpl) 值:[有 15363 个组织( s) 找到。] 但是,当我尝试“信息”级别时,一切都找到了。下面是示例代码: String s = "找到 ${organizationCount} 个组织。" Logger.getLogger(this.class.getName()).info(s) 请注意,当我将 'info' 替换为 'debug' 时,我遇到了该异常。
        • @firnnauriel:看起来您遇到了重载调试方法与 groovy 类型魔法的问题。尝试使用 + 而不是 GString 将字符串构建为“传统”Java 字符串。
        • 你也可以使用 Log4j,它会在参数上调用 toString(),所以不管它是 GString 还是 String。只需导入 org.apache.log4j.Logger 而不是 JUL Logger。
        【解决方案4】:

        Log4j 是 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
        

        希望对您有所帮助....

        【讨论】:

          【解决方案5】:

          我已经在使用 Logback 的 grails 3.1.8 中完成了这项工作。

          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"
             }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-17
            • 1970-01-01
            • 2016-01-30
            • 1970-01-01
            • 1970-01-01
            • 2017-06-27
            相关资源
            最近更新 更多