如何替换 Spring boot 2.0 中的默认日志记录系统



我将把一个slf4j实现(公司中基于logback的框架(集成到Spring boot 2.0中。在我的进度中,我发现默认的回落依赖项与我自己的依赖项冲突。以这种方式spring-boot-starter-logging模块中排除:

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

冲突的错误消失了,但似乎 Spring Boot 使用我的库作为 SLF4j 日志记录实现,就像以前一样,我的自定义函数中的任何内容都不起作用。

我想知道是否有办法用我的代码替换日志记录系统以使其工作,但 spring.io 网站上没有找到有关此内容的参考资料。

要禁用 Spring Boot 的 LoggingSystem(默认为 LogbackLoggingSystem(,您需要设置以下系统属性

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

这是 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html 和 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html 供参考。

删除 Spring-boot-starter-web 项目中的登录默认依赖项

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>

我们需要在类路径中放置一个名为以下属性文件之一的文件,然后重新启动项目

  1. log4j2-弹簧.xml
  2. log4j2.xml

错误

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

由具有 log4j-sl4j-impl 和 log4j-to-SL4J 的调用循环生成。

您可以解决排除 log4j 到 slf4j 的问题:

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

你只提供了你的pom.xml文件的一部分。问题可能是,可能存在从其他依赖项回退的传递依赖项。因此,您还必须在该依赖项中进行与 spring-boot-starter 相同的排除。现在,为了知道哪些依赖项都具有这种传递性,您可以打印pom.xml文件的依赖项树。为此,请转到包含pom.xml的文件夹。打开命令提示符。并键入 mvn 依赖项:树

最新更新