SpringCloud 2021.0.4 / Springboot 2.6.11集成 slf4j + logback 与 Swagger3

本文介绍了如何解决SpringBoot 2.6.11与Swagger3的版本冲突问题,以及如何正确配置logback,包括pom.xml中的依赖设置、logback-spring.xml配置和application.yml的调整。同时,详细阐述了Swagger3的配置步骤,包括Swagger3Config、WebMvcConfig的代码示例和application.yml的路径访问方式设定。
摘要由CSDN通过智能技术生成


前言

相信很多框架搭建的人都遇到过这样的问题:Springboot 2.6.x版本与 swagger3 不兼容,还得降低Springboot的版本,如果真的按网上说的将Springboot版本降了的话,总感觉有点本末倒置了。但是如果不降低Springboot的话,Swagger3还会报日志不兼容的问题,各种类找不到。

如何解决它们之间的版本冲突呢,今天就给大家一个完美的解决方案。


一、环境说明

  • JDK8
  • SpringCloud 2021.0.4
  • Springboot 2.6.11
  • Swagger 3
  • logback 使用Spring自带的,再配上logback-class即可
  • 配置文件使用 logback-spring.xml,并且支持控制台彩色打印

二、logback配置

1.pom配置

<!-- 以下三项依赖必须有,版本从父工程继承即可,这里使用的都是2.6.11 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Swagger3 依赖,只需这一个就够了, 版本也可以从父工程继承,这里直接写出来,以免大家不清楚-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

<!-- logback 日志依赖,只需这一个就够了,版本同样可以从父工程继承,这里也直接给大家写出来了-->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

这里需要 着重说明的是logback日志的版本,为什么选用1.2.3 ? 不知道大家有没有注意到SpringCloud 自带的logback-core的版本,它的版本是1.2.11,如下图,理论上选用的class版本最好不要超过1.2.11,也不要太低了,我个人感觉还是1.2.3比较完美
logback-core版本

2.logback-spring.xml

注意文件名最好不要改,就使用logback-spring.xml,其放放置的位置保持和application.yml 或 application.propertis 同一个目录,也就是classpath下,如下图:
logback-spring的位置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <contextName>logback-spring</contextName>

    <!-- 指定日志输出路径 -->
    <property name="logging.path" value="../logs" />

    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="color" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%color(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %color(${LOG_LEVEL_PATTERN:-%5p}) %color(${PID:- }){magenta} %color(---){faint} %color([%15.15t]){faint} %color(%-40.40logger{39}){cyan} %color(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!--输出到控制台-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--输出到文档-->
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在记录的日志文档的路径及文档名 -->
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yezhijing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值