Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J parameterized logging using varargs method

Tags:

java

slf4j

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!

like image 931
Manjabes Avatar asked Jul 18 '13 06:07

Manjabes


People also ask

Which logging framework does SLF4J use?

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

Does SLF4J use Commons logging?

The classes for commons-logging will be provided by jcl-over-slf4j.

Can we use SLF4J without log4j?

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.

Which is better SLF4J or log4j?

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.


1 Answers

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)

like image 56
ZachOfAllTrades Avatar answered Oct 03 '22 17:10

ZachOfAllTrades