feat: 增加 HTTP 请求和响应的日志记录

This commit is contained in:
xiongxiaoyang 2024-09-13 22:48:00 +08:00
parent d63be23aca
commit c46864bbb6
4 changed files with 62 additions and 2 deletions

View File

@ -25,6 +25,7 @@
<redisson.version>3.19.1</redisson.version>
<spring-boot-admin.version>3.0.0-M1</spring-boot-admin.version>
<springdoc-openapi.version>2.5.0</springdoc-openapi.version>
<logbook.version>3.9.0</logbook.version>
</properties>
<dependencies>
@ -204,6 +205,11 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-starter</artifactId>
<version>${logbook.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,37 @@
package io.github.xxyopen.novel.core.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.logbook.Logbook;
import static org.zalando.logbook.core.Conditions.*;
/**
* Logbook 配置
*
* @author xiongxiaoyang
* @date 2024/9/13
*/
@Configuration
public class LogbookConfig {
@Bean
public Logbook logbook() {
return Logbook.builder()
.condition(exclude(
// 忽略 OPTIONS 请求
requestWithMethod("OPTIONS"),
// 忽略 /actuator 以及其子路径Spring Boot Actuator 提供的端点的请求
requestTo("/actuator/**"),
// 忽略 Swagger 文档路径
requestTo("/swagger-ui/**"),
requestTo("/v3/api-docs/**"),
// 忽略二进制文件请求
contentType("application/octet-stream"),
// 忽略文件上传请求
contentType("multipart/form-data")
))
.build();
}
}

View File

@ -30,7 +30,7 @@ server:
--- #---------------------数据库配置---------------------------
spring:
datasource:
url: jdbc:mysql://localhost:3306/novel_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: test123456
# ShardingSphere-JDBC 配置
@ -229,6 +229,19 @@ spring:
class: javax.net.ssl.SSLSocketFactory
fallback: false
--- #----------------------Logbook配置-----------------------------
logbook:
format:
# 输出格式
style: http
obfuscate:
headers:
# 隐藏 Authorization 头信息
- Authorization
parameters:
# 隐藏密码参数
- password
--- #---------------------自定义配置----------------------------
novel:
# 跨域配置
@ -237,7 +250,7 @@ novel:
allow-origins:
- http://localhost:1024
- http://localhost:8080
# JWT密钥
# JWT 密钥
jwt:
secret: E66559580A1ADF48CDD928516062F12E
# XSS 过滤配置

View File

@ -62,6 +62,10 @@
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.zalando.logbook" level="TRACE" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
</springProfile>
<springProfile name="prod">