Search code examples
mavenlogbackslf4jlogback-classic

Not able to use logback version 1.5.3 in maven project


I've created a simple maven project in which I've added logback-classic, logback-core version 1.5.3 and slf4j-api 2.0.12 with JDK11. But when I run my main class I'm getting this warning/error msg.

**SLF4J(W): No SLF4J providers were found. SLF4J(W): Defaulting to no-operation (NOP) logger implementation SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details. Hello World! **

Folder structure: enter image description here

In pom.xml I've added dependency of logback-classic, logback-core version and slf4j-api.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.logbacklearn</groupId>
  <artifactId>mavenproj</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>mavenproj</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.12</version>
</dependency>
   
<dependency>
    <groupId>ch.qos.logback.access</groupId>
    <artifactId>common</artifactId>
    <version>2.0.0</version>
  </dependency> 

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.5.3</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.3</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-examples -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-examples</artifactId>
    <version>1.2.13</version>
</dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

My logback.xml looks like

<?xml version="1.0" encoding="UTF-8" ?>
 
<configuration scan="true" scanPeriod="3 seconds">
 
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
 
  <appender name="STDOUT"
            class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
        %d{HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</layout>
</appender>
 
  <appender name="FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>logFile.log</File>
<rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
        logFile.%d{yyyy-MM-dd_HH-mm}.log.zip
</FileNamePattern>
</rollingPolicy>
 
    <layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
        %d{HH:mm:ss,SSS} [%thread] %-5level %logger{32} - %msg%n
</Pattern>
</layout>
</appender>
 
  <root>
<level value="DEBUG"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
 
  <!--<include file="/tmp/logback-demo.xml"/>-->
 
</configuration>

My Main class looks like:

package com.logbacklearn.mavenproj;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 *
 */
public class App 
{
    static org.slf4j.Logger logger = LoggerFactory.getLogger(App.class);

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        logger.info("logger is working");
    }
}

When I debug value for logger it is showing me NOPLogger even though I added the dependency of logback-core, logback-classic to it. enter image description here

Logs are not getting printed on console? Am I missing something with newer version of logback or slf4j?

I have tried couple of things like doing mvn clean install again. Creating target folder freshly. Somehow it is not recognizing the dependency of logback-classic jar.

Also I'm seeing this first time, classic & junit jar under maven dependencies are greyed out. What is the reason behind that? enter image description here


Solution

  • The warning you mentioned tells the story:

    SLF4J(W): No SLF4J providers were found.  
    SLF4J(W): Defaulting to no-operation (NOP) logger implementation
    SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
    

    No SLF4J providers could be found.

    In your pom.xml, logback-classic dependency has the test scope. Thus, it is not on the classpath of your application. Removing the test scope will solve the issue.

    The classpath shown by your IDE mentions log4j 1.2.17. You do not need log4j 1.2.17 to use logback-classic.

    As for logback-access in your pom.xml, you should probably remove it. Logback-access integrates with Servlet containers such as Jetty or Tomcat to provide HTTP-access log functionality. This is distinct from application logging that logback-classic and SLF4J provide. Moreover, logback-access usually requires a very different setup than logback-classic, as logback-access needs to be integrated with your Servlet container's HTTP-logging.