Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.1.0.RC1, startup failed by Log4j2Metrics bean creation when third party library depends on log4j-core #14883

Closed
sis-yoshiday opened this issue Oct 18, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@sis-yoshiday
Copy link

commented Oct 18, 2018

Minimum dependencies for reproduction

dependencies {
  implementation('org.springframework.boot:spring-boot-starter-actuator')
  implementation('org.springframework.boot:spring-boot-starter-web')

  implementation('org.apache.logging.log4j:log4j-core')// if remove it, works fine
}

Part of stacktrace

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'log4j2Metrics' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.micrometer.core.instrument.binder.logging.Log4j2Metrics]: Factory method 'log4j2Metrics' threw exception; nested exception is java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:625) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:455) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1462) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1419) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1265) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1197) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.resolveStream(DefaultListableBeanFactory.java:1917) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.orderedStream(DefaultListableBeanFactory.java:1911) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryPostProcessor.asOrderedList(MeterRegistryPostProcessor.java:80) ~[spring-boot-actuator-autoconfigure-2.1.0.RC1.jar:2.1.0.RC1]
	at org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryPostProcessor.getConfigurer(MeterRegistryPostProcessor.java:72) ~[spring-boot-actuator-autoconfigure-2.1.0.RC1.jar:2.1.0.RC1]
	at org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryPostProcessor.postProcessAfterInitialization(MeterRegistryPostProcessor.java:64) ~[spring-boot-actuator-autoconfigure-2.1.0.RC1.jar:2.1.0.RC1]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1749) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	... 63 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.micrometer.core.instrument.binder.logging.Log4j2Metrics]: Factory method 'log4j2Metrics' threw exception; nested exception is java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
	... 85 common frames omitted
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
	at io.micrometer.core.instrument.binder.logging.Log4j2Metrics.<init>(Log4j2Metrics.java:56) ~[micrometer-core-1.1.0-rc.1.jar:1.1.0-rc.1]

Workaround

implementation('some-third-party-library') {
  exclude group: 'org.apache.logging.log4j', module: 'log4j-core'
}

In this case, org.apache.logging.log4j.LogManager#getContext(boolean) returns not org.apache.logging.log4j.core.LoggerContext but org.apache.logging.slf4j.SLF4JLoggerContext.
So Log4j2Metrics constructor crashed.

@wilkinsona

This comment has been minimized.

Copy link
Member

commented Oct 18, 2018

Thanks for the detailed report. We need to change the auto-configuration to back off when Log4J2's core jar is on the classpath, but it's actually Log4J to SLF4J that's being used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.