8

I'm using import lombok.extern.slf4j.Slf4j; for my class, and here's my log statement:log.info("{} : {} - {}", String1, String2, String3);

But it fails to compile and complaining the above line:java.lang.String cannot be converted to org.slf4j.Marker

Any ideas please?

Fisher Coder
  • 3,278
  • 12
  • 49
  • 84

3 Answers3

4

I guess that you are willing to use info(String format, Object... arguments) and are wondering why the method that is really called is info(Marker marker, String format, Object arg1, Object arg2).

This is related to Most Specific Method selection and Identify Potentially Applicable Methods.

As you have exactly four parameters, out of which three matches perfectly, the info(Marker marker, String format, Object arg1, Object arg2) method must be considered as "potentially matching".

You should read the documentation about variable arity parameters to get more details.

Kraal
  • 2,779
  • 1
  • 19
  • 36
0

I just had this same issue. I understand the problem, thank you Kraal, but a work-around like below would have been helpful.

log.info("{} : {} - {}", 
    new Object[] {String1, String2, String3});
Steve Gelman
  • 874
  • 9
  • 16
0

In my case, IntelliJ was picking up the Jar "slf4j-api-1.6.1.jar" by mistake, whereas I was expecting my code to be using "slf4j-api-1.7.10.jar".

There is a brief (but kind of subtle) discussion of why this is in the SLF4J FAQ. The idea behind the change is that the creators of Log4J made Log4J use Marker objects instead of Strings, while earlier betas used just Object. It seems like they changed it to be more permissive. The accepted answer calls out that wrong method call gets selected when the classpath has the wrong jar.

If you're using Lombok, it gets more confusing too, because the @Slf4j annotation interferes with the type used here.

Once I removed the 1.6.1 Jar from my classpath, and put in the 1.7.10 Jar instead, compilation started working again.

Jason D
  • 8,023
  • 10
  • 33
  • 39