gpt4 book ai didi

json - Json 格式的 API 日志记录

转载 作者:行者123 更新时间:2023-12-04 08:21:13 25 4
gpt4 key购买 nike

我想将 API 请求/响应记录为 json 格式。

预期的 LogEntry 类似于

{ 
"timestamp" : "...",
"level" : "DEBUG",
"headers" : [
"header1" : "value1",
"header2" : "value2",
"header3" : "value3"
],
"requestPayload" : "<Request Json>" // prefereablly as sub-document, worst case string is fine.
"labels" : [ //key fields which can be used for searching the logentry
"searchField1" : "....",
"searchField2" : "....",
"searchField3" : "...."
]
}

我的问题是:

使用 Logback,如何将嵌套字段(例如上面示例中的 header 、标签、requestPaylod)记录为 json 子文档。我尝试过 MDC,但仅限于
'String, String' 仅映射,并将第一级之后的所有字段视为字符串。

我讨厌为此编写自定义记录器,并希望使用经过验证的日志记录框架(logback/log4j)的优点来控制日志记录级别、时间戳日志事件等。

最佳答案

对于请求/响应日志,请尝试 Logbooklogstash-logback-encoder .您将能够使用标记来添加结构化内容。

为了在 JSON 结构中添加“您自己的”字段,我编写了一个您可能会感兴趣的代码生成器,它添加了构建器支持:json-log-domain

关于json - Json 格式的 API 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45682787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com