15
votes

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.

6
You have probably a difference between the two Spring Boot application. You should show at least their pom.xml. These could be helpful.davidxxx
Did you make a mistake with the version number? Logback 1.7.1 does not existAndy Wilkinson
@AndyWilkinson: Right - it was a typo. I changed it in the postChristian Triebstein
@davidxxx: I've been thoroughly checking both pom files for differences. Of course there are some given the fact that it is two different projects. The updated (and from my viewpoint) relevant part of the config converning spring boot was updated above - and that does not show any differences in both configsChristian Triebstein

6 Answers

25
votes

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.

7
votes

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

logging:
  config: classpath:logback-spring.xml
5
votes

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.

1
votes

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.

0
votes

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.

0
votes

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>