Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J varargs interprets first string as marker

When using

log.trace("with name {}, duration {}, repetitions {}", name, duration, repetitions);

SLF4J complains as follows

[javac] sourcefile.java:105: error: incompatible types: String cannot be converted to Marker
[javac] log.trace("with name {}, duration {}, repetitions {}",
[javac] ^
[javac] Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
[javac] 1 error

Using

log.trace("with name {}, duration {}, repetitions {}",
      new Object[]{name, duration, repetitions});

solves the problem, yet seems kludgey. (Especially since the API allows varargs).

Going by this answer seems to say that upgrading to SLF4J 1.7 would solve the problem, yet the android-slf4j is at 1.6.1.

Is there a way to use the varargs constructor in SLF4J for Android? Is there an alternative?

like image 248
serv-inc Avatar asked Nov 01 '15 22:11

serv-inc


2 Answers

Try solving your problem with following sample code:

log.trace("with name {0}, duration {1}, repetitions {2}", name, duration, repetitions);
like image 33
avocato Avatar answered Sep 22 '22 06:09

avocato


Seems like a bug in the API.

Varargs are used, but for the Marker methods there are also defined methods with 1, 2 and 3 arguments.

log.trace(Marker, Object...)
log.trace(Marker, Object)
log.trace(Marker, Object, Object)
log.trace(Marker, **Object**, Object, Object)

However the String APIs, only have varargs and 1 and 2 arg methods.

log.trace(String, Object...)
log.trace(String, Object)
log.trace(String, Object, Object)

For me Varargs do work with 1, 2, or 4, arguments.

Its only a problem with 3 arguments.

Its fixed in 1.7 by changing the highlighted Object to a String.

like image 185
teknopaul Avatar answered Sep 22 '22 06:09

teknopaul