如何从日志中的日志请求中排除特定的 属性 或数据类型?
How to exclude a specific property or data type from log request in logbook?
我有一个 spring 引导项目并使用 logbook 库来记录请求和响应。
在 REST API 服务之一中,属性数据类型之一是 byte[]
,因此当客户端发送请求时,由于该数据类型,它会在控制台中打印大量日志,即不是我想要的。
我在文档中看到您可以排除某些路径或内容类型,但不能按名称或数据类型排除。
这里是文档建议的配置方式:
Logbook logbook = Logbook.builder()
.condition(exclude(
requestTo("/health"),
requestTo("/admin/**"),
contentType("application/octet-stream"),
header("X-Secret", newHashSet("1", "true")::contains)))
.build();
您可以在配置 Logbook
实例时将 BodyReplacer
(s) 用作 RequestFilter
(s) 来混淆您的正文内容。
二进制数据
对于二进制数据,即当 HTTP Content-Type 是以下之一时:
- application/octet-stream
- application/pdf
- 音频/*
- 图片/*
- 视频/*
您可以按如下方式使用 BodyReplacers#binary
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.binary())) // which will replace body content with `<binary>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.binary())) // does the same when logging HTTP responses
// other configuration properties
.build();
多部分数据
当 HTTP 内容类型 的类型为:
- 多部分/*
您可以按如下方式使用 BodyReplacers#multipart
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.multipart())) // which will replace body content with `<multipart>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.multipart())) // does the same when logging HTTP responses
// other configuration properties
.build();
流式内容数据
当数据是块流时,即 HTTP Content-Type 是以下类型之一:
- application/json-seq
- application/x-json-stream
- application/stream+json
- text/event-stream
您可以按如下方式使用 BodyReplaces#stream
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.stream())) // which will replace body content with `<stream>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.stream())) // does the same when logging HTTP responses
// other configuration properties
.build();
tmarwen 的回答很好,但我想您只想忽略特定的属性或数据类型,而不是 HTTP 内容类型。如果是这样,那么您可以按照以下步骤忽略名称中的特定 属性:
.bodyFilter(jsonPath("$.YourSpecificPropertyName").delete())
或
.bodyFilter(jsonPath("$.YourSpecificPropertyName").replace("ReplaceMessage"))
这是文档中的完整示例:
假设您有这样的请求:
{
"id": 1,
"name": "Alice",
"password": "s3cr3t",
"active": true,
"address": "Anhalter Straße 17 13, 67278 Bockenheim an der Weinstraße",
"friends": [
{
"id": 2,
"name": "Bob"
},
{
"id": 3,
"name": "Charlie"
}
],
"grades": {
"Math": 1.0,
"English": 2.2,
"Science": 1.9,
"PE": 4.0
}
}
通过应用这些配置:
Logbook logbook = Logbook.builder()
.bodyFilter(jsonPath("$.password").delete())
.bodyFilter(jsonPath("$.active").replace("unknown"))
.bodyFilter(jsonPath("$.address").replace("X"))
.bodyFilter(jsonPath("$.name").replace(compile("^(\w).+"), "."))
.bodyFilter(jsonPath("$.friends.*.name").replace(compile("^(\w).+"), "."))
.bodyFilter(jsonPath("$.grades.*").replace(1.0))
.build();
变成:
{
"id": 1,
"name": "Alice",
"active": "unknown",
"address": "XXX",
"friends": [
{
"id": 2,
"name": "B."
},
{
"id": 3,
"name": "C."
}
],
"grades": {
"Math": 1.0,
"English": 1.0,
"Science": 1.0,
"PE": 1.0
}
}
我有一个 spring 引导项目并使用 logbook 库来记录请求和响应。
在 REST API 服务之一中,属性数据类型之一是 byte[]
,因此当客户端发送请求时,由于该数据类型,它会在控制台中打印大量日志,即不是我想要的。
我在文档中看到您可以排除某些路径或内容类型,但不能按名称或数据类型排除。
这里是文档建议的配置方式:
Logbook logbook = Logbook.builder()
.condition(exclude(
requestTo("/health"),
requestTo("/admin/**"),
contentType("application/octet-stream"),
header("X-Secret", newHashSet("1", "true")::contains)))
.build();
您可以在配置 Logbook
实例时将 BodyReplacer
(s) 用作 RequestFilter
(s) 来混淆您的正文内容。
二进制数据
对于二进制数据,即当 HTTP Content-Type 是以下之一时:
- application/octet-stream
- application/pdf
- 音频/*
- 图片/*
- 视频/*
您可以按如下方式使用 BodyReplacers#binary
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.binary())) // which will replace body content with `<binary>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.binary())) // does the same when logging HTTP responses
// other configuration properties
.build();
多部分数据
当 HTTP 内容类型 的类型为:
- 多部分/*
您可以按如下方式使用 BodyReplacers#multipart
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.multipart())) // which will replace body content with `<multipart>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.multipart())) // does the same when logging HTTP responses
// other configuration properties
.build();
流式内容数据
当数据是块流时,即 HTTP Content-Type 是以下类型之一:
- application/json-seq
- application/x-json-stream
- application/stream+json
- text/event-stream
您可以按如下方式使用 BodyReplaces#stream
替换器:
Logbook logbook = Logbook.builder()
.requestFilter(RequestFilters.replaceBody(BodyReplacers.stream())) // which will replace body content with `<stream>` string in all incoming HTTP requests
.responseFilter(ResponseFilters.replaceBody(BodyReplacers.stream())) // does the same when logging HTTP responses
// other configuration properties
.build();
tmarwen 的回答很好,但我想您只想忽略特定的属性或数据类型,而不是 HTTP 内容类型。如果是这样,那么您可以按照以下步骤忽略名称中的特定 属性:
.bodyFilter(jsonPath("$.YourSpecificPropertyName").delete())
或
.bodyFilter(jsonPath("$.YourSpecificPropertyName").replace("ReplaceMessage"))
这是文档中的完整示例:
假设您有这样的请求:
{
"id": 1,
"name": "Alice",
"password": "s3cr3t",
"active": true,
"address": "Anhalter Straße 17 13, 67278 Bockenheim an der Weinstraße",
"friends": [
{
"id": 2,
"name": "Bob"
},
{
"id": 3,
"name": "Charlie"
}
],
"grades": {
"Math": 1.0,
"English": 2.2,
"Science": 1.9,
"PE": 4.0
}
}
通过应用这些配置:
Logbook logbook = Logbook.builder()
.bodyFilter(jsonPath("$.password").delete())
.bodyFilter(jsonPath("$.active").replace("unknown"))
.bodyFilter(jsonPath("$.address").replace("X"))
.bodyFilter(jsonPath("$.name").replace(compile("^(\w).+"), "."))
.bodyFilter(jsonPath("$.friends.*.name").replace(compile("^(\w).+"), "."))
.bodyFilter(jsonPath("$.grades.*").replace(1.0))
.build();
变成:
{
"id": 1,
"name": "Alice",
"active": "unknown",
"address": "XXX",
"friends": [
{
"id": 2,
"name": "B."
},
{
"id": 3,
"name": "C."
}
],
"grades": {
"Math": 1.0,
"English": 1.0,
"Science": 1.0,
"PE": 1.0
}
}