spring boot-SpringBoot MDC仅用于下一个日志
发布时间:2022-05-19 00:45:49 252
相关标签:
关于SpringBoot应用程序和MDC日志的小问题。
我有一个非常简单的SpringBoot web处理程序:
@GetMapping(path = "/mdcOnlyToThisLogPlease")
public Mono mdcOnlyToThisLogPlease(@RequestBody Book book) {
MDC.setContextMap(Map.of("bookName", book.getTitle(), "bookAuthor", book.getAuthor()));
LOGGER.info("I would like to have a log with bookName and bookValue as first level key for the MDC Json log, and only for this log please");
return bookService.doSomething(book.getTitle(), book.getAuthor());
}
在这里,我想在 json 中记录书名和作者作为第一级键,我想得到这个:
{
@timestamp: 2022-02-22T02:22:22.222Z
@version: 1
bookName: someName
bookAuthor: someAuthor
level: INFO
level_value: 20000
logger_name: MyClass
message: I would like to have a log with bookName and bookValue as first level key for the MDC Json log, and only for this log please
spanId: e58ad767e34525de
thread_name: reactor-http-epoll-1
traceId: ccf32f0b5d210aa2
}
我认为这段代码可以做到这一点。不幸的是,它还记录了后面的每一个日志的密钥!
例如,我还在 bookService.doSomething() 的日志、健康检查日志等中看到...
{
@timestamp: 2022-05-02T04:31:30.648Z
@version: 1
bookName: someName
bookAuthor: someAuthor
level: INFO
level_value: 20000
logger_name: reactor.netty.http.server.AccessLog
message: 0:0:0:0:0:0:0:1%0 - - [02/May/2022:04:31:30 +0000] "GET /health/readiness HTTP/2.0" 200 5088 19
thread_name: reactor-http-epoll-1
}
{
@timestamp: 2022-05-02T04:33:30.648Z
@version: 1
bookName: someName
bookAuthor: someAuthor
level: INFO
level_value: 20000
logger_name: bookService
message: Log from book service. I do not wish to see bookName and bookAuthor as keys
thread_name: reactor-http-epoll-1
}
{
@timestamp: 2022-05-02T04:40:48.742Z
@version: 1
bookName: someName
bookAuthor: someAuthor
level: INFO
level_value: 20000
logger_name: reactor.netty.http.server.AccessLog
message: 0:0:0:0:0:0:0:1%0 - - [02/May/2022:04:40:48 +0000] "GET /health/liveness HTTP/2.0" 200 5088 25
thread_name: reactor-http-epoll-1
}
我可以问一下如何添加密钥,但只能添加下一个/一个日志吗?
非常感谢。
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报