java.lang.String cannot be converted to org.slf4j.Marker
Asked Answered
S

3

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?

Sweetmeat answered 26/1, 2017 at 18:45 Comment(0)
B
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.

Boatright answered 14/2, 2017 at 16:36 Comment(0)
D
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});
Diffusion answered 20/1, 2021 at 15:50 Comment(0)
N
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.

Navigator answered 21/3, 2021 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.