Spring Boot ignoring logback-spring.xml

前端 未结 6 480
慢半拍i
慢半拍i 2021-02-05 16:31

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

相关标签:
6条回答
  • 2021-02-05 16:48

    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 讨论(0)
  • 2021-02-05 16:59

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

    logging:
      config: classpath:logback-spring.xml
    
    0 讨论(0)
  • 2021-02-05 17:05

    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.

    0 讨论(0)
  • 2021-02-05 17:07

    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 讨论(0)
  • 2021-02-05 17:08

    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>
    
    0 讨论(0)
  • 2021-02-05 17:12

    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.

    0 讨论(0)
提交回复
热议问题