如何使用 Spring Sleuth 记录 MDC? [英] How to log MDC with Spring Sleuth?
问题描述
我有一个基于 Spring Boot + sleuth 的应用程序.一切都按预期工作.我现在有这样的日志:
I have a Spring boot + sleuth based application. All works as expected. I have for now logs like this:
2017-05-04 17:55:52.226 INFO [alert,692d0eeca479e216,c3c8b680dc29ad02,false] 17292 --- [cTaskExecutor-1] c.k.a.b.s.alert.impl.AlertServiceImpl : Alert state to process: xxx
现在,我想将自定义 MDC 添加到我的日志中,例如合同参考.我想要这样的日志:
Now, I want to add custom MDC to my log like the contract reference for example. I want to have logs like this:
2017-05-04 17:55:52.226 INFO [alert,692d0eeca479e216,c3c8b680dc29ad02,false] [CONTRACT_REF] 17292 --- [cTaskExecutor-1] c.k.a.b.s.alert.impl.AlertServiceImpl : Alert state to process: xxx
我尝试了各种方法都没有成功:
I tried various things with no success:
- 使用 Spring Sleuth Tracer 添加标签;
- 使用 MDC.put(xxx, xxx) 将 logging.pattern.level=%5p %mdc 添加到我的 application.properties 文件中
如何将自定义 MDC/标签添加到我的日志中?
How can I add custom MDC/tags to my log?
推荐答案
对于 2.x 之前的版本,您必须创建自己的 SpanLogger
实现.最简单的方法是扩展 Slf4jSpanLogger
并提供您自己的代码来添加/更新和删除 MDC 上下文中的条目.然后您可以更改您的日志记录模式,这样您的日志将包含他们需要的内容.
For versions before 2.x, You have to create your own implementation of a SpanLogger
. The easiest way will be to extend the Slf4jSpanLogger
and provide your own code to add / update and remove the entries from MDC context. Then you can change your logging pattern and that way your logs will contain what they need.
这篇关于如何使用 Spring Sleuth 记录 MDC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!