简体   繁体   English

有一个适用于log4j2的适配器可以在slf4j上运行吗?

[英]Is there an adapter for log4j2 to work over slf4j?

I have a third party library (elasticsearch 5.x) which uses log4j2. 我有一个使用log4j2的第三方库(elasticsearch 5.x)。 My application uses slf4j. 我的应用程序使用slf4j。 Is there an adapter for version 2 of log4j, similar to the version 1 adapter (log4j-over-slf4j)? 是否有适用于log4j版本2的适配器,类似于版本1适配器(log4j-over-slf4j)?

Just to clarify: I don't want to actually use log4j or log4j2 as the actual implementation (binding). 只是为了澄清:我不想实际使用log4j或log4j2作为实际的实现(绑定)。 I use logback for that. 我使用logback。 So I need a log4j2 to slf4j adapter, not an slf4j binding. 所以我需要一个log4j2到slf4j适配器,而不是slf4j绑定。

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: 我还应该提一下,我已经找到并尝试了这个库(在2.0-beta版本中): https//logging.apache.org/log4j/2.0/log4j-to-slf4j/index.html但它给了我这个错误:

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 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 最新版本目前为2.8.2

You should include log4j-to-slf4j-2.x.jar and ensure to not include log4j-slf4j-impl-2.x.jar . 您应该包含 log4j-to-slf4j-2.x.jar并确保不包含 log4j-slf4j-impl-2.x.jar See Log4j to SLF4J Adapter for more details. 有关更多详细信息,请参阅Log4j到SLF4J适配器

From https://logging.apache.org/log4j/2.0/faq.html 来自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. 当应用程序调用Log4j 2 API并且您希望将日志记录调用路由到SLF4J实现时,可以使用log4j-to-slf4j适配器jar。

在此输入图像描述

Slf4j project does not provide bridge from log4j v2 to Slf4j (it hasn't been mentioned in https://www.slf4j.org/legacy.html ). Slf4j项目没有提供从log4j v2到Slf4j的桥接(在https://www.slf4j.org/legacy.html中没有提到它)。

Maven dependencies: Maven依赖:

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

Gradle dependency: Gradle依赖:

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 包列表: https//mvnrepository.com/artifact/org.apache.logging.log4j/log4j-to-slf4j

I think you can choose two libraries. 我想你可以选择两个库。 You can take it from: http://www.java2s.com/Code/Jar/l/Downloadlog4jjar.htm or https://logging.apache.org/log4j/2.0/download.html or other. 您可以从以下网址获取: http//www.java2s.com/Code/Jar/l/Downloadlog4jjar.htmhttps://logging.apache.org/log4j/2.0/download.html或其他。

Log4j2 itself bundles a slf4j implementation (log4j-slf4j-impl-2.x.jar) Log4j2本身捆绑了一个slf4j实现 (log4j-slf4j-impl-2.x.jar)

This is one of the jars in the Log4j2 distribution . 这是Log4j2 发行版中的一个jar。


Update after the question was clarified: 问题澄清后更新:

Log4j2 includes a log4j-to-slf4j bridge “. Log4j2包括一个log4j到slf4j桥 “。 This is what you need to route Log4j2 logging to another slf4j implementation. 这是将Log4j2日志记录路由到另一个slf4j实现所需的。

The error mentioned is likely a problem of incompatible versions but the question doesn't mention version numbers so it's hard to say. 提到的错误可能是版本不兼容的问题,但问题没有提到版本号,所以很难说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM