1

I am new to slf4j, In my Project I am using it for getting Logs.

I am using InteliJ IDE.

Here is my code


package com.sample;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class TestMainclass 

{

    static Logger logger = LoggerFactory.getLogger(TestMainclass.class);

    public static void main(String[] args) {

        System.out.println("before");

        logger.debug("hello");

        logger.info("hello");

        System.out.println("after");

        }

}

here is the dependency

    testCompile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.30'

Everything working fine in normal setup but when I build my code, and run that jar with junit-platform-console-standalone-1.5.2.jar , below error is coming:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hardik Rana
  • 476
  • 1
  • 6
  • 16

2 Answers2

1

The idea of the SLF4J project was to create a common API for the many log frameworks that exist in the Java world.

So this just gives you interfaces with methods to call so your code can compile and run, but not a log framework that slf4j can then delegate work to. This is normally done by having both slf4j and the chosen log framework jar on the classspath at runtime and (if needed) a bridge to connect the two. This is described in detail in the slf4j documentation.

You have chosen slf4j-jdk14 which uses the logging framework built into the Java runtime, but apparently it is not present on the classpath when junit run, causing the error.

I would suggest looking at the classpath at the time you see the error, to see what slf4j jars are there.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

SLF4J is a logging API. It requires an implementation. There are several choices; Log4j 2, Logback, log4j 1.x (not recommneded), or java util logging (not recommended). My personal preference is log4j 2 in which case you would need 3 jars - log4j-slf4j-impl, log4j-api, and log4j-core. These will all use a group of org.apache.logging.log4j and the latest version is 2.13.2.

rgoers
  • 8,696
  • 1
  • 22
  • 24