当前位置: 首页 > 知识库问答 >
问题:

如何在Groovy上配置Logback SMTP appender?

彭鹭洋
2023-03-14

我正在尝试使用Groovy为我的独立Java应用程序配置Logback SMTP appender。我的logback.groovy文件内容如下:

    import ch.qos.logback.classic.PatternLayout
    import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.classic.net.SMTPAppender
    import ch.qos.logback.core.ConsoleAppender

    import static ch.qos.logback.classic.Level.INFO

    def common_log_date_format = "{MM/dd HH:mm:ss:SSS}"
    def common_log_conversionpattern = "[%d${common_log_date_format}] [%t] [%c{0}] [%p]: %m%n"

    appender("STDOUT", ConsoleAppender) {
      encoder(PatternLayoutEncoder) {
        pattern = "${common_log_conversionpattern}"
      }
    }

    appender("EMAIL", SMTPAppender) {
      smtpHost = "xxxx"
      to = "yyyy"
      from = "zzzz"
      subject = "[${app.name}] [%marker] [%p]"
      asynchronousSending = false
      layout(PatternLayout) {
        pattern = "${common_log_conversionpattern}"
      }
    }

    root(INFO, ["STDOUT", "EMAIL"])

但此配置不起作用,它在应用程序启动过程中失败:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.NullPointerException: Cannot get property 'name' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:168)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
at Script1$_run_closure2.doCall(Script1.groovy:22)
at Script1$_run_closure2.doCall(Script1.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at ch.qos.logback.classic.gaffer.ConfigurationDelegate.appender(ConfigurationDelegate.groovy:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod.invoke(MixinInstanceMetaMethod.java:53)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:313)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
at Script1.run(Script1.groovy:18)
at Script1$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at ch.qos.logback.classic.gaffer.GafferConfigurator.run(GafferConfigurator.groovy:64)
at ch.qos.logback.classic.gaffer.GafferConfigurator$run.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at ch.qos.logback.classic.gaffer.GafferConfigurator.run(GafferConfigurator.groovy:40)
at ch.qos.logback.classic.gaffer.GafferUtil.runGafferConfiguratorOn(GafferUtil.java:43)
at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:65)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at com.revere.edm.job.Loader.<init>(Loader.java:13)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at com.revere.shell.Shell.execute(Shell.java:233)
at com.revere.shell.Shell.main(Shell.java:64)

我无法理解这条信息的原因。如果我从config-application中删除SMTP Appender配置部分,就可以正常工作并记录日志。如何使SMTP附加与groovy配置一起工作?

我的环境是:Ubuntu 12.04、logback Classic/Core 1.0.13、Groovy-all jar版本2.2.0-RC-3、Janino 2.6.1、javax.mail1.4。

共有1个答案

袁旻
2023-03-14

这是我针对groovy&grails 3.2.0 appender和smtp gmail的测试和生产环境的解决方案:

import grails.util.BuildSettings
import grails.util.Environment
import ch.qos.logback.classic.html.HTMLLayout
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.net.SMTPAppender
import ch.qos.logback.core.ConsoleAppender

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%level %logger - %msg%n"
    }
}

appender("EMAIL", SMTPAppender) {
    smtpHost = "smtp.gmail.com"
    smtpPort = 587
    STARTTLS = true
    username = "email@gmail.com"
    password = "password"
    to = "emailRecepter@domain.com"
    from = "email@gmail.com"
    subject = "Logback Sam error"
    asynchronousSending = false
    layout(HTMLLayout) {
        pattern = "%level %logger - %msg%n"
    }
    println "Setting EMAIL property"
}

def targetDir = BuildSettings.TARGET_DIR
if (Environment.isDevelopmentMode() && targetDir != null) {
    appender("FULL_STACKTRACE", FileAppender) {
        file = "${targetDir}/stacktrace.log"
        append = true
        encoder(PatternLayoutEncoder) {
            pattern = "%level %logger - %msg%n"
        }
    }
    logger("StackTrace", ERROR, ['FULL_STACKTRACE'], false)
    root(ERROR, ['STDOUT', 'FULL_STACKTRACE'])

}
else {
    root(ERROR, ['STDOUT','EMAIL'])
}
 类似资料:
  • 问题内容: 如何为此操作创建超时:? 问题答案: 我相信正确的方法将是这样的: 当然,这是我使用Oracle的地方,但是我希望这可以给您一个想法。

  • 我试图使用RatPack.groovy中的块合并服务器和数据库配置,但是在试图创建数据源时为空。 PostgreSmodule.groovy RatPack.groovy

  • 我正在运行IntelliJ IDEA 2017.2.3。我通过Homebrew(OS X)安装了Groovy 2.4.12。当我打开Groovy源文件(或)时,我会得到以下内容: 未为模块“my-module”配置Groovy SDK。...。.配置Groovy SDK... 单击“配置Groovy SDK...”引导我进入以下对话框:

  • 问题内容: 我正在开发一个需要HTTPS的Spring Java应用程序,并且按照Spring的指南配置SSL。 我的application.properties文件包含以下内容: 运行该应用程序时,控制台将输出Tomcat started on port(s): 8443 (https),但是对服务器的任何请求都将导致一个0 NO RESPONSE状态。 我想念什么? 问题答案: 问题似乎是指定

  • 我在java上配置Spring web mvc6.It在weblogic 12.1.3上成功运行 当我将java-6更改为java-8时,会引发以下错误 没有[weblogic.server.ServerService]类型的合格bean,原因是:org.springframework.beans.factory。NoSuchBeanDefinitionException:找不到依赖项的类型为[w

  • 我正在尝试使用groovy配置文件进行GraalVM编译的应用程序使用的logback。 当运行它并正常编译它时,一切都很好,但当我试图针对Graal进行编译时,我面临着一些我无法理解如何解决的问题。 因此,在没有任何特定配置传递给Graal的情况下,我在编译过程中遇到以下错误: 我尝试了非常懒惰的方法,通过这样做来允许在构建期间初始化类: 这只会导致其他各种与日志存储相关的类需要这个选项,所以我