Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an adapter for log4j2 to work over slf4j?

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

like image 409
Nazaret K. Avatar asked Apr 28 '17 16:04

Nazaret K.


2 Answers

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.

like image 136
mmdemirbas Avatar answered Sep 27 '22 20:09

mmdemirbas


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.

like image 45
Remko Popma Avatar answered Sep 27 '22 22:09

Remko Popma