java - 如何从日志中的日志请求中排除特定属性或数据类型?
问题描述
我有一个 spring boot 项目并使用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();
解决方案
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).+"), "$1."))
.bodyFilter(jsonPath("$.friends.*.name").replace(compile("^(\\w).+"), "$1."))
.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
}
}
推荐阅读
- amazon-web-services - 键包含点时是否可以设置映射?
- python-3.x - Sqlalchemy 查询外键
- sql - BigQuery 谷歌分析自动化问题
- reactjs - 当我打印 PokemonData 时,它是未定义的
- android-studio - 如何使用云存储(Android Studio)存储谷歌地图标记?
- android - Android中的DB打开失败错误,可能是由于DB文件位置
- javascript - 在 Electron 应用程序中刷新 div 的内容
- jquery - 单击功能在第一次单击时不起作用
- html - div之间垂直线的CSS
- c# - 如何将 x:Name 添加到通过 xmlreader 创建的控件