【问题标题】:How to configure log4j for a grails plugin?如何为 grails 插件配置 log4j?
【发布时间】:2023-03-25 01:45:01
【问题描述】:

这是我的插件 Config.groovy 中的 log4j 配置:

log4j = {       
    appenders {
        console name: "stdout",
                layout: pattern(conversionPattern: "%c{2} %m%n")
    }

    debug 'grails.app.services'
}

我有一个记录服务,但我没有在我的标准输出上看到任何记录器打印,只是为了确保我同时使用了 println 和 log.info,但我只看到了 println 输出。

我已经看到了,但没有帮助。

How do I configure logging for a grails plugin?

【问题讨论】:

    标签: grails log4j grails-plugin


    【解决方案1】:

    您需要为根记录器配置日志级别和附加程序,这将用作所有其他记录器的默认值。假设您想使用错误作为默认级别,并且仅将日志发送(附加)到控制台,请执行以下操作:

    log4j = {       
        appenders {
            console name: "stdout", layout: pattern(conversionPattern: "%c{2} %m%n")
        }
    
    
        root {
            // by default, log at the ERROR level and send logs to the console
            error 'stdout'
        }
    
        // override the default level to DEBUG for service classes
        debug 'grails.app.services'
    }
    

    【讨论】:

    • 如果您在应用程序中安装了插件,那么您需要在 应用程序 中配置 log4j,而不是插件。插件的 log4j 配置在安装到应用程序时被忽略
    • 好的,很高兴知道,但是当我的应用程序中的 log4j 配置不起作用时,我将它添加到插件中。顺便说一句,应用程序服务可以记录,但插件服务不能。非常感谢
    【解决方案2】:
    'org.codehaus.groovy.grails.plugins'
    

    将此代码添加到 log4j 配置中的错误。并且您的插件将被记录。 例如我的 log4j 配置:

    log4j = {
    // Example of changing the log pattern for the default console appender:
    //
    appenders {
    
        console name: 'stdout', layout: pattern(conversionPattern: '%d [%t] %-5p (%c) - %m%n')
    
    }
    
    root {
        info 'stdout', 'file'
        additivity = true
    }
    error '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 <-You nedd this one
            'org.codehaus.groovy.grails.orm.hibernate',      // hibernate integration
            'org.springframework',
            'org.hibernate',
            'net.sf.ehcache.hibernate'
    

    }

    【讨论】:

      猜你喜欢
      • 2017-09-24
      • 2010-12-18
      • 1970-01-01
      • 2012-10-19
      • 2012-07-28
      • 2011-02-24
      • 2015-04-28
      • 2011-05-30
      • 1970-01-01
      相关资源
      最近更新 更多