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

Embedded Broker does not work with Log4j2 #319

Closed
john2home opened this issue Nov 26, 2021 · 3 comments
Closed

Embedded Broker does not work with Log4j2 #319

john2home opened this issue Nov 26, 2021 · 3 comments

Comments

@john2home
Copy link

Expected behavior

Log4j 2 SLF4J Binding can be used for SLF4J API

Actual behavior

if you exclude logback-classic in gradle because you want to use log4j2 you get an exception on start

2021-11-26 08:55:04.694 UTC|${sys:mea.location}|ERROR| c.h.u.UncaughtExceptionHandler:32  Uncaught exception in thread 'embedded-hivemq-state-change-executor'.
java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
	at com.hivemq.HiveMQServer.bootstrap(HiveMQServer.java:109) ~[hivemq-community-edition-embedded-2021.3.jar:2021.3]
	at com.hivemq.embedded.internal.EmbeddedHiveMQImpl.stateChange(EmbeddedHiveMQImpl.java:147) ~[hivemq-community-edition-embedded-2021.3.jar:2021.3]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.core.joran.spi.JoranException
	at jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[?:?]
	at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[?:?]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]
	... 5 more

if you use logback-classic and log4j2 you get

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/repo/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.14.1/9a40554b8dab7ac9606089c87ae8a5ba914ec932/log4j-slf4j-impl-2.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repo/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
2021-11-26 09:00:02.833 UTC|${sys:mea.location}|INFO | c.h.e.i.EmbeddedHiveMQImpl:75  Setting default authentication behavior to ALLOW ALL
2021-11-26 09:00:08.360 UTC|${sys:mea.location}|INFO | c.h.e.i.EmbeddedHiveMQImpl:141  Starting EmbeddedHiveMQ.
2021-11-26 09:00:08.374 UTC|${sys:mea.location}|INFO | c.h.c.i.SystemInformationImpl:146  HiveMQ version: 2021.3
Exception in thread "main" java.util.concurrent.CompletionException: java.lang.ClassCastException: class org.apache.logging.slf4j.Log4jLoggerFactory cannot be cast to class ch.qos.logback.classic.LoggerContext (org.apache.logging.slf4j.Log4jLoggerFactory and ch.qos.logback.classic.LoggerContext are in unnamed module of loader 'app')
	at java.base/java.util.concurrent.CompletableFuture.reportJoin(CompletableFuture.java:412)
	at java.base/java.util.concurrent.CompletableFuture.join(CompletableFuture.java:2044)
	at hivemq.EmbeddedBroker.main(EmbeddedBroker.java:33)
Caused by: java.lang.ClassCastException: class org.apache.logging.slf4j.Log4jLoggerFactory cannot be cast to class ch.qos.logback.classic.LoggerContext (org.apache.logging.slf4j.Log4jLoggerFactory and ch.qos.logback.classic.LoggerContext are in unnamed module of loader 'app')
	at com.hivemq.bootstrap.LoggingBootstrap.getRootLogger(LoggingBootstrap.java:145)
	at com.hivemq.bootstrap.LoggingBootstrap.prepareLogging(LoggingBootstrap.java:67)
	at com.hivemq.HiveMQServer.bootstrap(HiveMQServer.java:109)
	at com.hivemq.embedded.internal.EmbeddedHiveMQImpl.stateChange(EmbeddedHiveMQImpl.java:147)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)

To Reproduce

Steps

Use embedded broker example found on this site

Reproducer code

build.gradle

def log4jVersion = '2.14.1'
dependencies {
	implementation("com.hivemq:hivemq-community-edition-embedded:2021.3") {
		exclude group: 'ch.qos.logback', module: 'logback-classic'
	}
	implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
	implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
	runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")
}

Details

  • Affected HiveMQ CE version(s): 2021.3
  • Used JVM version: Adoptium 11.0.12+7 64bit
@DC2-DanielKrueger
Copy link
Contributor

We do not support LOG4j2. I'll close this ticket.

@Zomis
Copy link

Zomis commented Apr 6, 2023

I think closing this ticket and only saying "We do not support Log4j2" is the wrong way to go. The solution is to depend on slf4j-api and not a specific implementation of slf4j (e.g. Logback or Log4j2).

Solving #366 should solve this issue as well.

@pglombardo
Copy link
Contributor

Hi @Zomis - that's a fair point. Now that the request is filed in #366 and linked here - we'll make sure to update both issues once any updates happen.

Apologies for the brevity in closing this issue previously without a proper explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants