java.lang.NoSuchMethodError: 'void org.apache.logging.slf4j.Log4jLoggerFactory.<init>(org.apache.logging.slf4j.Log4jMarkerFactory)'

huangapple go评论68阅读模式
英文:

java.lang.NoSuchMethodError: 'void org.apache.logging.slf4j.Log4jLoggerFactory.<init>(org.apache.logging.slf4j.Log4jMarkerFactory)'

问题

I'm currently on an Apache Flink job that provides log4j2 as the default logging implementation at runtime (see here). For flexibility, SLF4J should be used. Running the entire job in Flink logs properly, but during unit tests using JUnit 5, there are no logs shown in IntelliJ.

For debugging purposes, it would be necessary to have the log statements also during unit test execution.

I have created a minimal example project.

The Problem:

I have traced it down to the inclusion of org.apache.flink:flink-test-utils:1.16.1, which results in the following error:

Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: &#39;void org.apache.logging.slf4j.Log4jLoggerFactory.&lt;init&gt;(org.apache.logging.slf4j.Log4jMarkerFactory)&#39;
	at org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:54)
	at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183)
	at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170)
	at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455)
	at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:416)
	at bmw.pcmasfd.zip.flink.udf.BuildGroupContextTest.&lt;clinit&gt;(BuildGroupContextTest.java:27)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)

Context:

IntelliJ IDEA 2022.3.3 RC (Ultimate Edition)
Java version: 11.0.18 (Azul)
Flink version: 1.16.1

pom.xml(v1):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;minimal-logging-example&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;

        &lt;flink.version&gt;1.16.1&lt;/flink.version&gt;

    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;version&gt;1.18.26&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- Flink --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-streaming-java&lt;/artifactId&gt;
            &lt;version&gt;1.16.1&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-runtime&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-core&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;!-- TESTING --&gt;
        &lt;!--
        only including this dependency leads to:
        java.lang.NoSuchMethodError: &#39;void org.apache.logging.slf4j.Log4jLoggerFactory.&lt;init&gt;(org.apache.logging.slf4j.Log4jMarkerFactory)&#39;
        --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-test-utils&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;!-- Logging --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
            &lt;version&gt;2.17.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
            &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
            &lt;version&gt;2.0.6&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-slf4j2-impl&lt;/artifactId&gt;
            &lt;version&gt;2.20.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;!-- Testing --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.13.2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;


<details>
<summary>英文:</summary>

im currently on a apache flink job which provides log4j2 as default logging implementation at its runtime ([see here](https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/deployment/advanced/logging/)).
For flexibility SLF4J should be used. 
Running the whole job in flink logs properly, but during Unit Test using JUnit5 there are no logs shown in IntelliJ.

For debugging purposes it would be necessary to have the log statements also during unit test execution. 

I have made a minimal [example project](https://github.com/iLem0n/minmal-logging-example)

**The Problem:**

I have drilled it down to inclusion of `org.apache.flink:flink-test-utils:1.16.1` which results to the following:

Exception in thread "main" java.lang.NoSuchMethodError: 'void org.apache.logging.slf4j.Log4jLoggerFactory.<init>(org.apache.logging.slf4j.Log4jMarkerFactory)'
at org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:54)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170)
at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:416)
at bmw.pcmasfd.zip.flink.udf.BuildGroupContextTest.<clinit>(BuildGroupContextTest.java:27)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)


**Context:** 

IntelliJ IDEA 2022.3.3 RC (Ultimate Edition)
Java version: 11.0.18 (Azul)
Flink version: 1.16.1


**pom.xml**(v1):
```xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;org.example&lt;/groupId&gt;
&lt;artifactId&gt;minimal-logging-example&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;properties&gt;
&lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
&lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;flink.version&gt;1.16.1&lt;/flink.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;version&gt;1.18.26&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- Flink --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
&lt;artifactId&gt;flink-streaming-java&lt;/artifactId&gt;
&lt;version&gt;1.16.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
&lt;artifactId&gt;flink-runtime&lt;/artifactId&gt;
&lt;version&gt;${flink.version}&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
&lt;artifactId&gt;flink-core&lt;/artifactId&gt;
&lt;version&gt;${flink.version}&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- TESTING --&gt;
&lt;!--
only including this dependency leads to:
java.lang.NoSuchMethodError: &#39;void org.apache.logging.slf4j.Log4jLoggerFactory.&lt;init&gt;(org.apache.logging.slf4j.Log4jMarkerFactory)&#39;
--&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
&lt;artifactId&gt;flink-test-utils&lt;/artifactId&gt;
&lt;version&gt;${flink.version}&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- Logging --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
&lt;version&gt;2.17.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
&lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
&lt;version&gt;2.0.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-slf4j2-impl&lt;/artifactId&gt;
&lt;version&gt;2.20.0&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- Testing --&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;4.13.2&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;5.9.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.skyscreamer&lt;/groupId&gt;
&lt;artifactId&gt;jsonassert&lt;/artifactId&gt;
&lt;version&gt;1.5.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

EDIT:
pom.xml(v2):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;minimal-logging-example&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;

        &lt;flink.version&gt;1.16.1&lt;/flink.version&gt;

    &lt;/properties&gt;

    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                &lt;artifactId&gt;log4j-bom&lt;/artifactId&gt;
                &lt;version&gt;2.20.0&lt;/version&gt;
                &lt;type&gt;pom&lt;/type&gt;
                &lt;scope&gt;import&lt;/scope&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;version&gt;1.18.26&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- Flink --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-streaming-java&lt;/artifactId&gt;
            &lt;version&gt;1.16.1&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-runtime&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-core&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;!-- TESTING --&gt;
        &lt;!--
        only including this dependency leads to:
        java.lang.NoSuchMethodError: &#39;void org.apache.logging.slf4j.Log4jLoggerFactory.&lt;init&gt;(org.apache.logging.slf4j.Log4jMarkerFactory)&#39;
        --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-test-utils&lt;/artifactId&gt;
            &lt;version&gt;${flink.version}&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                    &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
                &lt;/exclusion&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                    &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
                &lt;/exclusion&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                    &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
        &lt;!--
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-test-utils-junit&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
            &lt;artifactId&gt;flink-streaming-java&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
            &lt;classifier&gt;tests&lt;/classifier&gt;
        &lt;/dependency&gt;
        --&gt;

        &lt;!-- Logging --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
            &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
            &lt;version&gt;2.0.6&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-slf4j2-impl&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;!-- Testing --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.13.2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.9.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.skyscreamer&lt;/groupId&gt;
            &lt;artifactId&gt;jsonassert&lt;/artifactId&gt;
            &lt;version&gt;1.5.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

答案1

得分: 1

The flink-test-utils 依赖项引入了一个旧版本的 log4j-slf4j-impl 作为传递依赖项(通过 flink-test-utils-junit)。

这可能是一个 bug,您应该报告它:作者忘记将 Log4j2 依赖项放在 test 范围内。库不应该将日志后端包括为 compile/runtime 依赖项,而应该将该选择留给库用户。

同时,您可以简单地添加一些排除项:

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils</artifactId>
<version>1.16.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>

备注: log4j-corelog4j-slf4j2-impl 的版本应该保持同步(当前 log4j-corelog4j-api 2.17.1 引入您的项目,而 log4j-slf4j2-impl 需要版本 2.20.0)。最简单的方法是在依赖管理中使用 log4j-bom

备注 2:

为了灵活性,应该使用 SLF4J。

这是一个常见的误解/过时观念:当引入 SLF4J 时,它提供了 Logback 的所有功能,同时允许用户使用传统的日志后端(Log4j 1.x、java.util.logging)。

当约 10 年后引入了 Log4j 2.x API 时,它提供了 Log4j 2.x Core 的所有功能,同时允许用户使用其他日志后端(Logback、java.util.logging)。

通过在 Log4j 2.x Core 中使用 SLF4J,您限制了可用的日志 API 方法,而实际上并没有增加日志后端的选择。

编辑: flink-test-utils 依赖项的另一个问题是它依赖于 flink-coreflink-runtime 的测试 jar,其中包含一个 log4j2-test.properties 文件。这是在测试期间禁用日志记录的文件。

现在您有几种解决方法:

  1. 测试 jar 通常只包含对项目本身有用的代码。您可以尝试从 flink-test-utils 中排除 flink-coreflink-runtime
  2. 向测试资源中添加一个 log4j2-test.properties 文件,并依赖于通常情况下测试启动器在其依赖项之前将测试类放在类路径上。不幸的是,属性配置格式的优先级高于所有其他格式,因此您不能只使用另一种格式。
  3. 向测试资源中添加一个 log4j2-test.* 配置文件(例如 log4j2-test.xml),以及一个具有以下内容的 log4j2.component.properties 文件:
log4j2.configurationFile=log4j2-test.xml
英文:

The flink-test-utils dependency pulls an old version of log4j-slf4j-impl as transitive dependency (through flink-test-utils-junit).

This is probably a bug and you should report it: the authors forgot to put the Log4j2 dependencies in the test scope. Libraries should not include a logging backend as compile/runtime dependency and leave that choice to the library user.

In the meantime you can simply add some exclusions:

  &lt;dependency&gt;
&lt;groupId&gt;org.apache.flink&lt;/groupId&gt;
&lt;artifactId&gt;flink-test-utils&lt;/artifactId&gt;
&lt;version&gt;1.16.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;

Remark: The versions of log4j-core and log4j-slf4j2-impl should be in sync (right now log4j-core pulls log4j-api 2.17.1 into your project, whereas log4j-slf4j2-impl requires version 2.20.0). The easiest way is to use log4j-bom in your dependency management.

Remark 2:
> For flexibility SLF4J should be used.

That is a common misconception/anachronism: when SLF4J was introduced, it offered all the features of Logback, while allowing users to use a legacy logging backend (Log4j 1.x, java.util.logging).

When the Log4j 2.x API was introduced about 10 years later, it offered all the features of Log4j 2.x Core, while allowing users to use other logging backends (Logback, java.util.logging).

By using SLF4J with Log4j 2.x Core, you limit the available logging API methods, while you don't really increase the choice of logging backends.

Edit: Another problem with the flink-test-utils dependency is that it depends on the tests jar of flink-core and flink-runtime, which contains a log4j2-test.properties file. This is the file that disables logging during testing.

Now you have several workarounds:

  1. The test jars usually contain code only useful for the project itself. You can try excluding flink-core and flink-runtime from flink-test-utils,

  2. Add a log4j2-test.properties file to your test resources and rely on the fact that usually test launchers place the test classes on the classpath before their dependencies. Unfortunately the properties configuration format has a higher priority than all other formats, so you can't just use another format.

  3. Add a log4j2-test.* configuration file (e.g. log4j2-test.xml) to your test resources and a log4j2.component.properties file with content:

    log4j2.configurationFile=log4j2-test.xml
    

huangapple
  • 本文由 发表于 2023年3月12日 07:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定