Christian Triebstein
Christian Triebstein

Reputation: 425

Spring Boot ignoring logback-spring.xml

I have 2 Spring Boot (1.4.1-RELEASE) console applications using Logback. Both configuration files are more or less identical, are located in my /src/main/resources folder and named logback-spring.xml.

Both projects include the maven dependency spring-boot-starter-logging in their pom.xml and fetch the logback Version 1.1.7.

The Spring Boot config as defined in both poms:

<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>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath />
</parent>

<groupId>d.m.v.app-a</groupId>
<artifactId>my-app-a</artifactId>
<version>1.0.16-SNAPSHOT</version>
<packaging>jar</packaging>  

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
</dependencies>

However, when running the applications one of them seems to fully ignore the logback configuration while the other picks it up like expected.

If I change the filename to logback.xml for the application that didn't work properly it suddenly works fine (even with the spring profiles that I'm using in them).

There is no apparent difference in any of the configurations involved (meaning the pom.xml, application.properties, etc.).

Does anybody know why that might be the case? I find this behaviour rather confusing.

Upvotes: 21

Views: 26652

Answers (6)

Divya Sanghi
Divya Sanghi

Reputation: 1

I faced the same issue and tried finding if any of logback.xml or logback-grrovy.xml etc. already in claspath, but was not able to find any

Then I tried some changes:

  1. Changed name from logback.xml to logback-spring.xml

  2. Removed following dependency from pom.xml Instead of

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.13</version>
        </dependency>
    

I tried this

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>

Upvotes: 0

granadaCoder
granadaCoder

Reputation: 27842

Ok.

I know this is a maven question. But you can 'translate' gradle talk into maven.

The gist of the idea is:

In my root build.gradle file.

I had to exclude these items. The way gradle does it, is this is a "global to this project" type of exclude, so it gets them ALL in one place. But you can do the same "excluding"/scrubbing in maven, it will just look different. The groupid/artifactid's are what is important.

allprojects {


    configurations {
     
        all {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-log4j2'
        }
    }


}

Then in my (top layer) build.gradle (I don't have a monolith, I have a project with multiple modules). I added these dependencies:

implementation group: 'ch.qos.logback', name: 'logback-classic', version: logbackClassicVersion
    
implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion

My variable values for the two versions happen to be (at this time of writing this post)

    slf4jVersion = "1.7.30"
    logbackClassicVersion = '1.2.3'

How do you find the 2 versions that play nice together?

For example, if you look at:

https://mvnrepository.com/artifact/ch.qos.logback/logback-classic/1.2.3

and find the "compile dependencies", you'll see that

logback-classic/1.2.3

has

org.slf4j » slf4j-api 1.7.25 1.7.30

so somewhere between 1.7.25 and 1.7.30 will play nice with logback-classic/1.2.3

There's probably a better way, but that's how I "get close" at the dependency game.

    slf4jVersion = "1.7.30"
    logbackClassicVersion = '1.2.3'

But you would be better going to maven repostitory or jcenter and finding latest version but also versions that of these 2 things that play nice with each other.

Upvotes: 0

Donald Choi
Donald Choi

Reputation: 371

I solved this problem by adding logging.config in application.yml

logging:
  config: classpath:logback-spring.xml

Upvotes: 19

Hans Schreuder
Hans Schreuder

Reputation: 753

To use Logback, you need to include it and spring-jcl on the classpath. The simplest way to do that is through the starters, which all depend on spring-boot-starter-logging. For a web application, you need only spring-boot-starter-web, since it depends transitively on the logging starter. If you use Maven, the following dependency adds logging for you:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

So remove the logging dependency it is redundant.

Upvotes: 1

ltuska
ltuska

Reputation: 721

I know it is somewhat old, but i had the same issue and figured it out... so the reason is simply that you have a logback.xml on your classpath (somewhere, not necessarily in your project which you start, in my case it was a dependency).

Take a look here: org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile)

set a breakpoint, then you will see.

If spring boot doesn't find any logback configurations ("logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml") on the classpath, logback-spring.xml will be picked up.

Upvotes: 36

kimy82
kimy82

Reputation: 4465

I would specify in application.properties the location of the config file like that.

logging.config=path

Spring might not be looking for this file name. Spring doc

They suggest using this name logback-spring.xml rather than just logback.xml

I would place the configuration in application.properties if possible.

Upvotes: 7

Related Questions