java - 使用 Logstash 编码器记录异常时如何包含 mdc 键
问题描述
我有logback-spring.xml
文件,其中有 json 控制台附加程序:
我包括 mdc 密钥requestId
:
但是,当记录运行时异常时,此 requestId 会丢失。当出现异常时,有没有办法可以在日志中打印 requestId?
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>log_datetime</timestamp>
<thread>thread</thread>
<logger>[ignore]</logger>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
<callerClass>class_name</callerClass>
<callerMethod>method_name</callerMethod>
<callerFile>[ignore]</callerFile>
<callerLine>line_num</callerLine>
</fieldNames>
<includeMdcKeyName>requestId</includeMdcKeyName>
<timestampPattern>yyyy-MM-dd HH:mm:ss.SSS</timestampPattern>
<includeCallerData>true</includeCallerData>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<maxLength>2048</maxLength>
<shortenedClassNameLength>20</shortenedClassNameLength>
<exclude>sun\.reflect\..*\.invoke.*</exclude>
<exclude>net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
<rootCauseFirst>true</rootCauseFirst>
<inlineHash>true</inlineHash>
</throwableConverter>
</encoder>
</appender>
<springProfile name="!local">
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</springProfile>
</configuration>
解决方案
推荐阅读
- python - 在 Pandas GroupBy 中查找组中的重复值
- php - 每次打印一个新的 & 其中包含“N”个元素,总数为“Q”
- button - 选择时如何在 UWP 中的按钮上显示边框
- javascript - 尝试获取数据时 GraphQL 中的空值
- javascript - reactjs中如何防止外部函数调用
- javascript - 如何通过提供循环结果来停止循环?
- python - Chrome 已关闭并出现错误错误:network_change_notifier_win.cc(141)] WSALookupServiceBegin 失败,原因为:0
- pyspark - 在 group 和 agg 之后显示所有 pyspark 列
- amazon-web-services - ask-cli, Ask deploy, UnrecognizedClientException: 请求中包含的安全令牌无效
- java - 使用递归除法的迷宫生成器