Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception thrown while using logback/slf4j

I am using slf4j 1.6.2 api jar (tried using 1.6.1 as well) - logback version is 0.9.29 (core & classic). I am using jdk1.6 on ubuntu. The exception I received is copied below.

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
    at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:112)
    at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:471)
    at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:427)
    at ch.qos.logback.classic.Logger.info(Logger.java:631)

I am also getting a message complaining about slf4j binding mismatch.

"SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11]"
like image 413
haider Avatar asked Aug 23 '11 18:08

haider


People also ask

Does SLF4J support Logback?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.

How do you log exception in SLF4J?

In presence of multiple parameters, if the last argument in a logging statement is an exception, then SLF4J will presume that the user wants the last argument to be treated as an exception instead of a simple parameter: logger. error("{}, {}!

Is SLF4J and Logback same?

Logback and SLF4J can be primarily classified as "Log Management" tools. According to the StackShare community, Logback has a broader approval, being mentioned in 4 company stacks & 9 developers stacks; compared to SLF4J, which is listed in 5 company stacks and 7 developer stacks.

Can SLF4J work without log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.


4 Answers

It very much looks like the version of slf4j-api.jar being loaded by the JVM has version 1.5.x. You surely have slf4j-api-1.5.x.jar on your class path (in addition to slf4j-api-1.6.2.jar). Check your class path.

like image 193
Ceki Avatar answered Nov 12 '22 23:11

Ceki


Adding the following dependencies might help :

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jul-to-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>log4j-over-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.7.7</version>
  </dependency>
like image 38
Trisha Chatterjee Avatar answered Nov 13 '22 01:11

Trisha Chatterjee


slf4j-api version does not match that of the binding:

An SLF4J binding designates an artifact such as slf4j-jdk14.jar or slf4j-log4j12.jar used to bind slf4j to an underlying logging framework, say, java.util.logging and respectively log4j.

Mixing different versions of slf4j-api.jar and SLF4J binding can cause problems. For example, if you are using slf4j-api-1.7.2.jar, then you should also use slf4j-simple-1.7.2.jar, using slf4j-simple-1.5.5.jar will not work.

NOTE From the client's perspective all versions of slf4j-api are compatible. Client code compiled with slf4j-api-N.jar will run perfectly fine with slf4j-api-M.jar for any N and M. You only need to ensure that the version of your binding matches that of the slf4j-api.jar. You do not have to worry about the version of slf4j-api.jar used by a given dependency in your project. You can always use any version of slf4j-api.jar, and as long as the version of slf4j-api.jar and its binding match, you should be fine.

At initialization time, if SLF4J suspects that there may be a api vs. binding version mismatch problem, it will emit a warning about the suspected mismatch.

Got from http://www.slf4j.org, I hope it can help.

like image 20
itro Avatar answered Nov 13 '22 00:11

itro


Also, you must have a many slf4j-api jars of versions mentioned in the []. Try keeping a single version of slf4j-api and the corresponding compatible slf4j-log4j jars in the classpath.

Mixing different versions of slf4j jars will always be troublesome

The NoSuchMethodError is due to the discovery of Methods-with-the-same-name more than once, probably coming from the different versions of the same jars

like image 33
Arun GK Avatar answered Nov 13 '22 00:11

Arun GK