使用springboot和logback获取基本日志记录的问题

时间:2016-02-24 17:35:03

标签: spring logging groovy

我在groovy中有一个简单的弹簧启动应用程序。这是我到目前为止设置的gradle依赖

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    compile 'org.easyrules:easyrules-core:2.2.1-SNAPSHOT'
    compile 'org.easyrules:easyrules-spring:2.2.1-SNAPSHOT'
    compile 'org.easyrules:easyrules-jmx:2.2.1-SNAPSHOT'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.springframework.boot:spring-boot-starter'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-logging'
    compile 'ch.qos.logback:logback-classic:1.1.5'
}

这会在1.7.13版本中引入slf4j-api,log4j-over-slf4j,jul-to-slf4j和jcl-over-slf4f

然后在我的帮助班中我有这个

@Component
@PropertySource ("classpath:application.properties")
class RulesHelper {

    private static final Logger log = LoggerFactory.getLogger(RulesHelper.class)

然后是像这样的bean def

@Bean
def rulesEngine () {
    assert jmxEnabled != null
    if ( !jmxEnabled ) {
        //return std rules engine
        println "returning std rules engine of type ${stdRulesEngine.getClass().name}"
        ruleEngine = stdRulesEngine
        return stdRulesEngine
    } else {
        println "returning jmx rules engine of type ${jmxRulesEngine.getClass().name}"
        //log.debug("hello from logger")
        //assert log

        ruleEngine = jmxRulesEngine
        return jmxRulesEngine  //JmxRulesExtends RulesEngine so should work
    }
}

如果我取消注释断言我得到一个错误,当我这样运行说它无法找到org.slf4j.event.LoggingEvent

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Object]: Factory method 'rulesEngine' threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/event/LoggingEvent
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/slf4j/event/LoggingEvent
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.codehaus.groovy.reflection.CachedClass$3$1.run(CachedClass.java:85)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:82)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue(CachedClass.java:80)
    at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getMethods(CachedClass.java:251)
    at groovy.lang.MetaClassImpl.populateMethods(MetaClassImpl.java:361)
    at groovy.lang.MetaClassImpl.fillMethodIndex(MetaClassImpl.java:340)
    at groovy.lang.MetaClassImpl.initialize(MetaClassImpl.java:3224)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:222)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:253)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:263)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:259)
    at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:855)
    at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:888)
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:880)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToBoolean(DefaultTypeTransformation.java:180)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.booleanUnbox(DefaultTypeTransformation.java:69)
    at org.softwood.easyrules.RulesHelper.rulesEngine(RulesHelper.groovy:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 27 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.slf4j.event.LoggingEvent

我在这里做错了什么 - 我认为这可以根据我读过的内容起作用

3 个答案:

答案 0 :(得分:4)

你知道什么 - 依赖管理问题 - 最新的springboot-starter-logging仅使用slf4j-api.1.7.13,

然而,如果你得到了我做SELECT tableB.Budget - (SELECT SUM(amount) FROM tableA) FROM tableB.Budget; 的最新回溯,那么在Api客户端librray中的某个地方就没有出口该类的问题 - 这就是它无法找到的原因。见logback issue/news

如果您明确添加依赖项

logback_classic.1.1.5

到gradle.build,刷新并重试它都开始按照大自然的预期工作。像往常一样,这些事情通常是悲伤的

答案 1 :(得分:1)

  

java.lang.NoClassDefFoundError:org / slf4j / event / LoggingEvent

Logback-classic版本1.1.4及更高版本需要slf4j-api版本1.7.15或更高版本。 对于类路径中较早的slf4j-api.jar,尝试内省由logback版本1.1.4或更高版本返回的Logger实例将导致NoClassDefFoundError类似于下面显示的内容。

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/event/LoggingEvent
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
    at java.lang.Class.privateGetPublicMethods(Class.java:2571)
    at java.lang.Class.getMethods(Class.java:1429)
    at java.beans.Introspector.getPublicDeclaredMethods(Introspector.java:1261)
    at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1122)
    at java.beans.Introspector.getBeanInfo(Introspector.java:414)
    at java.beans.Introspector.getBeanInfo(Introspector.java:161)

在类路径中放置slf4j-api.jar版本1.7.15或更高版本应解决此问题。 请注意,此问题仅发生在logback版本1.1.4及更高版本中,其他绑定(如slf4j-log4j,slf4j-jdk14和slf4j-simple)不受影响。

答案 2 :(得分:1)

缺少相应的Maven依赖项是从https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.7.15那里获得的

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.15</version>
        </dependency>