13
votes

I have a third party library (elasticsearch 5.x) which uses log4j2. My application uses slf4j. Is there an adapter for version 2 of log4j, similar to the version 1 adapter (log4j-over-slf4j)?

Just to clarify: I don't want to actually use log4j or log4j2 as the actual implementation (binding). I use logback for that. So I need a log4j2 to slf4j adapter, not an slf4j binding.

I should also mention that I have found and tried this library (in 2.0-beta version): https://logging.apache.org/log4j/2.0/log4j-to-slf4j/index.html but it gives me this error:

Caused by: java.lang.AbstractMethodError: org.apache.logging.slf4j.SLF4JLoggerContextFactory.getContext(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Object;Z)Lorg/apache/logging/log4j/spi/LoggerContext;
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426)
at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:49)
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:105)
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:72)
at org.elasticsearch.common.component.AbstractComponent.<init>(AbstractComponent.java:37)
at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:98)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:99)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:124)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:258)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:125)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:111)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:101)

EDIT: Ok.. so I guess I was just blind yesterday and I was only seeing the beta version of this library. Therefore the answer is that there is such as adapter and it is here:

https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-to-slf4j https://logging.apache.org/log4j/2.0/log4j-to-slf4j/index.html

The latest version currently is 2.8.2

4
Please mention clearly what versions of all relevant libraries you are using when you see this error.Remko Popma
Yes, it was an old version. I got it to work with the latest (2.8.2). ThanksNazaret K.

4 Answers

1
votes

Log4j2 itself bundles a slf4j implementation (log4j-slf4j-impl-2.x.jar)

This is one of the jars in the Log4j2 distribution.


Update after the question was clarified:

Log4j2 includes a log4j-to-slf4j bridge “. This is what you need to route Log4j2 logging to another slf4j implementation.

The error mentioned is likely a problem of incompatible versions but the question doesn't mention version numbers so it's hard to say.

6
votes

You should include log4j-to-slf4j-2.x.jar and ensure to not include log4j-slf4j-impl-2.x.jar. See Log4j to SLF4J Adapter for more details.

2
votes

From https://logging.apache.org/log4j/2.0/faq.html

You can use the log4j-to-slf4j adapter jar when your application calls the Log4j 2 API and you want to route logging calls to a SLF4J implementation.

enter image description here

Slf4j project does not provide bridge from log4j v2 to Slf4j (it hasn't been mentioned in https://www.slf4j.org/legacy.html).

Maven dependencies:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.11.0</version>
</dependency>

Gradle dependency:

compile "org.apache.logging.log4j:log4j-to-slf4j:2.10.0"

Note that above package has transitive dependency on:

org.slf4j:slf4j-api:1.7.25
org.apache.logging.log4j:log4j-api:2.10.0

List of packages: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-to-slf4j