Groovy Logback-自定义布局-错误无法实例化LoggerContext

时间:2018-10-04 16:43:39

标签: groovy logback logback-groovy

在尝试使用LINK提供的文档创建自定义布局时。运行grails应用程序时出现以下错误。

使用的IDE-Intellioj IDEA。 我也是新手。我缺少任何依赖项,因此无法解决utils.MyLayout类。

|Loading Grails 2.3.11
|Configuring classpath
Error |
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Error |
Reported exception:
Error |
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
**Script1.groovy: 8: unable to resolve class utils.MyLayout
@ line 8, column 1.**
import utils.MyLayout
^
1 error
Error |
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)

自定义布局类:

package utils
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.LayoutBase

class MyLayout extends LayoutBase<ILoggingEvent> {

@Override
public String doLayout(ILoggingEvent event) {
    StringBuffer sbuf = new StringBuffer(128)
    sbuf.append(event.getTimeStamp() - event.getLoggingContextVO.getBirthTime())
    sbuf.append(" ")
    sbuf.append(event.getLevel())
    sbuf.append(" [")
    sbuf.append(event.getThreadName())
    sbuf.append("] ")
    sbuf.append(event.getLoggerName())
    sbuf.append(" - ")
    sbuf.append(event.getFormattedMessage())
    sbuf.append(CoreConstants.LINE_SEP)
    return sbuf.toString()
    }
}

logback.groovy

import grails.util.Environment
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import utils.MyLayout

displayStatusOnConsole()

def displayStatusOnConsole() {
    System.out.println("Inside display status **************")
    statusListener OnConsoleStatusListener
}

appender("CONSOLE", ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        layout(MyLayout){pattern = "[%thread] - %msg%n"}
    }
}
if (Environment.isDevelopmentMode()) {
    root(INFO, ['CONSOLE'])
}

BuildConfig.groovy中的代码段

dependencies {
    // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
    test "org.grails:grails-datastore-test-support:1.0-grails-2.3"
    runtime 'ch.qos.logback:logback-classic:1.1.2'
    runtime 'ch.qos.logback:logback-core:1.1.2'
    runtime 'org.slf4j:slf4j-api:1.7.10'
}

....
/*
 * If the application is being run locally, set the Logback configuration file
 * system property.  This is necessary as Grails will attempt to compile the .groovy
 * file instead of simply adding it to the classpath "as is".  There is a corresponding
 * event handler in 'scripts/Events.groovy' that takes care of making sure that the
 * Groovy configuration file makes it into the WAR in its un-compiled state.
 */
if(new File("${basedir}/grails-app/conf/logback.groovy").exists()) {
    System.out.println(basedir)
    System.setProperty('logback.configurationFile', "${basedir}/grails-app/conf/logback.groovy")
}

0 个答案:

没有答案