19

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

Nazaret K.
  • 3,409
  • 6
  • 22
  • 31

3 Answers3

10

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.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
  • 1
    +1 Proper solution for modern log4j v2. Slf4j project does not provide bridge. It was done by Apache project itself. – gavenkoa Jul 18 '18 at 19:56
3

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.

30thh
  • 10,861
  • 6
  • 32
  • 42
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Sorry if I was not clean, but I don't want to use log4j or log4j2 as my implementation (slf4j binding). I use logback for that. I just want a log4j2 to slf4j adapter. See my edits above. – Nazaret K. Apr 29 '17 at 04:58
  • Thanks Remko, I just found the correct version of the log4j-to-slf4j adapter and got it to work. I just posted the answer to my question, but since I see you also posted the same answer now I will accept yours instead of mine. Thanks – Nazaret K. Apr 29 '17 at 05:24
2

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

gavenkoa
  • 45,285
  • 19
  • 251
  • 303