Elarbi Mohamed Aymen
Elarbi Mohamed Aymen

Reputation: 1710

Logback configuration file not loaded

I'm working on a maven project and I use slf4j to perform logging but my problem is my logback.xml configuration is not loaded and it won't take effect on my project. My logback.xml is placed in src/main/resources folder

Dependencies:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${logback.version}</version>
    </dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${jcloverslf4j.version}</version>
</dependency>

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XML>
<configuration>

    <!-- Appenders -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder 
            by default -->
        <encoder>
            <pattern>[%d{dd-MM-yyyy HH:mm:ss.SSS}] [%-5level] [%logger{36}.%M\(%line\)] - %msg %n</pattern>
        </encoder>
    </appender>

    <!-- Application logger -->
    <logger name="com.example">
        <level value="info" />
    </logger>


    <!-- Root Logger -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

Main class:

public class Main {

    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        logger.trace("hi");
        logger.debug("hi");
        logger.info("hi");
        logger.warn("hi");
        logger.error("hi");
    }
}

Expected:

09:34:27.298 [main] INFO com.example.entry.Main - hi
09:34:27.298 [main] WARN com.example.entry.Main - hi
09:34:27.298 [main] ERROR com.example.entry.Main - hi

Output:

09:34:27.296 [main] DEBUG com.example.entry.Main - hi
09:34:27.298 [main] INFO com.example.entry.Main - hi
09:34:27.298 [main] WARN com.example.entry.Main - hi
09:34:27.298 [main] ERROR com.example.entry.Main - hi

Upvotes: 6

Views: 16113

Answers (6)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

You xml config file is missing <configuration> root element:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" />
...

Upvotes: -1

BKE
BKE

Reputation: 573

It is likely, that logback is also included in one of your dependencies, and the build picks up the logback configuration from the dependency, not the one the you provided.

You can check:

mvn dependency:tree -Dverbose

If you find out, that logback is included multiple times in the result, then exclude it from the dependencies in pom.xml:

<exclusions>
    <exclusion>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </exclusion>
</exclusions>

Upvotes: 4

Elarbi Mohamed Aymen
Elarbi Mohamed Aymen

Reputation: 1710

Problem solved by deleting jar files from .m2 directory

Upvotes: 3

user944849
user944849

Reputation: 14951

Check the classpath, does it contain commons-logging as well as jcl-over-slf4j? If it does, exclude commons-logging and see if that works. I've had problems with apps when they have both dependencies, they seem to conflict.

Upvotes: 0

I think you should be using the following in your pom.XML

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${slf4j.version}</version>
</dependency>

instead of

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${jcloverslf4j.version}</version>
</dependency>

Upvotes: -1

Anton Tupy
Anton Tupy

Reputation: 969

According to documentation https://logback.qos.ch/manual/configuration.html try to replace

    <logger name="com.example">
        <level value="info" />
    </logger>

to

<logger name="com.example" level="INFO"/>

Upvotes: -1

Related Questions