I must be stupid or something, but I seem not to be able to use the varargs-utilizing parameterized logging methods of SLF4J. An example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggingTest {
@Test
public void loggingTest() {
Logger logger = LoggerFactory.getLogger(this.getClass());
int x = 0xdeadbeef;
long y = 0xdeadbeef;
try {
throw new Exception("This is a mighty exception!");
} catch(Exception e) {
logger.error("I wanna log {} and {} and {} with backtrace", x, y, 3, e);
}
}
}
On the logging method, eclipse produces such a warning:
The method error(String, Object, Object) in the type Logger is not applicable for the arguments (String, int, long, int, Exception)
and fails to compile.
However, if I change the logging call to:
logger.error("I wanna log {} and {} and {} with backtrace", new Object[]{x, y, 3, e});
It compiles and runs as expected (logging 3 "variables" and the exception stack trace).
The library versions are: slf4j-api-1.7.5.jar, slf4j-log4j12-1.7.5.jar and log4j-1.2.14.jar, if it makes any difference.
If anybody would point out the shortcomings of my thinking abilities, it'd be very much appreciated!
SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.
The classes for commons-logging will be provided by jcl-over-slf4j.
SLF4J is the API for Logback just like Log4j-api is the API for Log4j-core. When you want to use Log4j with SLF4J logger calls go to log4j-slf4j-impl then to Log4j-api and then to log4j-core. If you use the Log4j-api with Logback then Log4j-api calls go to log4j-to-slf4j, slf4j and then to Logback.
Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.
I did some additional investigation, and the only way to get a compile error for
logger.error("I wanna log {} and {} and {} with backtrace", x, y, 3, e);
and not for
logger.error("I wanna log {} and {} and {} with backtrace", new Object[]{x, y, 3, e});
is to use a version of the SLF4J API prior to 1.7 (in which support for varargs was introduced). You probably need to dig into your classpath (or server runtime?) to find where the following statement fails to be true:
The library versions are: slf4j-api-1.7.5.jar, slf4j-log4j12-1.7.5.jar and log4j-1.2.14.jar, if it makes any difference.
(because it certainly makes precisely the difference that you have observed)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With