简体   繁体   中英

“This version of SLF4J requires log4j version 1.2.12” warning with log4j 1.2.17

I have a Java Maven project whose dependencies include slf4j and its log4j adapter . I manage the versions of log4j , slf4j-log4j12 and slf4j-api to its newest versions according to http://mvnrepository.com and especially the log4j version of 1.2.17 is well over 1.2.12 but I still get the error

SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. 
    See also http://www.slf4j.org/codes.html#log4j_version 

which is totally unclear to me.

My Maven dependency management looks like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>apache-jena-libs</artifactId>
            <type>pom</type>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>
</dependencyManagement>

How can I get rid of the warning?

PS: I also get a java.lang.NoSuchMethodError: org.apache.log4j.Logger.isTraceEnabled() .

PPS: Because of your comments I remembered that the program has a "lib" folder which is not included in the classpath by Maven but by Eclipse itself so the conflicting dependencies must lie there. Sorry, I totally forgot that I guess that's my fault for mixing Maven with a lib folder. I guess I must try to convert as much of the libraries to maven dependencies. The strange thing though is just that even if I edit "Order and Export" to put the Maven dependencies at the top, the problem still occurs.

StaticLoggerBinder code in slf4j-log4j12 which gets loaded very early on performs a check to determine whether the TRACE level is available in log4j. Here is the code:

private StaticLoggerBinder() {
  loggerFactory = new Log4jLoggerFactory();
  try {
    Level level = Level.TRACE;
  } catch (NoSuchFieldError nsfe) {
    Util.report("This version of SLF4J requires log4j version 1.2.12 or later."+
                " See also http://www.slf4j.org/codes.html#log4j_version");
  }
}

It seems pretty airtight to me.

It may be that some other dependency is pulling in or actually embeds an earlier version of log4j. Some * -standalone.jar files are known to do that.

Check your class path at deployment time. Are you deploying in an app server? Is an older version of log4j on the server class path? In a java endorsed path?

You should define the <dependency> outside the <dependencyManagement> as the following: -

<dependencyManagement>
    <dependencies>
    ...
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.apache.jena</groupId>
        <artifactId>apache-jena-libs</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
</dependencies>

Please see Introduction to the Dependency Mechanism: Dependency Management for further information.

I hope this may help.

Changing comment to answer, since it appeared to solve the issue.

This is a classloader problem, maven built it correctly but is missing for dependencies. Could you try to provide the missing dependencies to maven? Otherwise I suggest you look in the Eclipse specific dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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