Not able to use logback version 1.5.3 in maven project

ghz 8months ago ⋅ 144 views

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

Answers

It seems that your Logback configuration and dependencies are correctly set up, but there might be an issue with the way your project is being built or executed. Here are a few steps you can take to troubleshoot the problem:

  1. Ensure that your logback.xml file is placed in the src/main/resources directory of your Maven project. This is the default location where Logback expects to find its configuration file.

  2. Make sure that your Maven dependencies are properly resolved. You can check this by running mvn clean compile and ensuring that there are no errors during the build process.

  3. Check the Maven dependency tree to verify that Logback and SLF4J dependencies are included in your project. You can do this by running mvn dependency:tree and inspecting the output to ensure that logback-core, logback-classic, and slf4j-api are listed as dependencies.

  4. Check if there are any conflicting dependencies in your project that might be causing issues with Logback. You can use the Maven Dependency Plugin to analyze your project's dependencies and identify any conflicts. Run mvn dependency:analyze to generate a report of potential conflicts.

  5. Ensure that your logback.xml configuration is valid and properly formatted. Any errors or typos in the configuration file could prevent Logback from functioning correctly.

  6. If you're running your application from an IDE like IntelliJ IDEA or Eclipse, make sure that the IDE's settings are not overriding the Maven dependencies or Logback configuration.

Regarding the grayed-out jars in your IDE, this typically indicates that the jars are not directly referenced by your project and are instead transitively included as dependencies of other libraries. This is normal behavior and should not affect the functionality of your project.