简体   繁体   中英

How can I replace the default logging system in spring boot 2.0

I'm going to integrate a slf4j implementation (framework in company based on logback) into spring boot 2.0. In my progress I found the default logback dependency is conflict to my own dependency. Excluded spring-boot-starter-logging module in this way:

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

the conflicting error disappeared but it seems like spring boot use my library as the slf4j logging implementation just the way before, nothing in my custom functions works.

I'm wondering if there is a way I can replace the logging system with my code to make it work but no references about this are found in spring.io website.

To disable Spring Boot's LoggingSystem, which defaults to LogbackLoggingSystem, you need to set the following system property

-Dorg.springframework.boot.logging.LoggingSystem=none 

This is in https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html and https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html for reference.

The error

Exception in thread "main" java.lang.StackOverflowError
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)

generated by call-loop with log4j-sl4j-impl and log4j-to-sl4j.

You can resolve excluding log4j-to-slf4j:

configurations {
        all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
    }

Remove login default dependency in the Spring-boot-starter-web project

dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

we need to place in the classpath a file named like one of the following properties file and restart the project

  1. log4j2-spring.xml
  2. log4j2.xml

You have given only a portion of your pom.xml file. The problem may be that, there may be a transitive dependency to logback from some other dependency. So you have to make the same exclusion in that dependency too as in the case of spring-boot-starter. Now in order to know which all dependencies have this transitivity, you can print the dependency tree of your pom.xml file. For that go to the folder that contains pom.xml. Open command prompt. And type mvn dependency: tree .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM