Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems with getting basic logging up with springboot and logback

i have a simple spring boot app in groovy. here are the gradle dependencies i have set so far

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

this brings in slf4j-api, log4j-over-slf4j, jul-to-slf4j, and jcl-over-slf4f at versions 1.7.13

then in my helper class i have this

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

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

followed by a bean def like this

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

if i uncomment out that assert i get an error when i run like this saying it cant find 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

what have i done wrong here - i thought this would work based on what i have read

like image 793
WILLIAM WOODMAN Avatar asked Feb 24 '16 17:02

WILLIAM WOODMAN


People also ask

How is Logback configured for logging in Spring Boot?

By default, Spring Boot picks up the native configuration from its default location for the system (such as classpath:logback. xml for Logback), but you can set the location of the config file by using the "logging. config" property.

Which logging framework is best for Spring Boot?

If you are using Spring Boot Starters, Logback will provide a good support for logging. Besides, Logback also provides a use of good support for Common Logging, Util Logging, Log4J, and SLF4J.

Does Spring Boot use Log4J instead of Logback?

Spring Boot supports Log4j 2 for logging configuration if it is on the classpath. If you are using the starters for assembling dependencies that means you have to exclude Logback and then include log4j 2 instead. If you aren't using the starters then you need to provide jcl-over-slf4j (at least) in addition to Log4j 2.

Is Log4J2 better than Logback?

So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks. Logback is still a good option, if performance is not your highest priority. Stackify's Application Performance Management tool, Retrace offers log management for your Java applications.


3 Answers

well what do you know - dependency management issue - the latest springboot-starter-logging is only using slf4j-api.1.7.13,

however if you get the latest logback which i did logback_classic.1.1.5 there was breaking issue somewhere in the Api client librray that didnt export the class - which is why it couldnt be found. see logback issue/news

if you explicitly add the dependency

...
compile 'ch.qos.logback:logback-classic:1.1.5'
compile 'org.slf4j:slf4j-api:1.7.16'

to gradle.build, refresh and retry it all starts to work as nature expected. Blimey painful as usual as these things normally are

like image 75
WILLIAM WOODMAN Avatar answered Oct 22 '22 04:10

WILLIAM WOODMAN


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

Logback-classic version 1.1.4 and later require slf4j-api version 1.7.15 or later. With an earlier slf4j-api.jar in the classpath, attempting introspection of a Logger instance returned by logback version 1.1.4 or later will result in a NoClassDefFoundError similar to that shown below.

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)

Placing slf4j-api.jar version 1.7.15 or later in the classpath should solve the issue. Note that this problem only occurs with logback version 1.1.4 and later, other bindings such as slf4j-log4j, slf4j-jdk14 and slf4j-simple are unaffected.

like image 26
xuweisi Avatar answered Oct 22 '22 05:10

xuweisi


corresponding maven dependency missing got it from 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>
like image 42
Ram Ghadiyaram Avatar answered Oct 22 '22 06:10

Ram Ghadiyaram