我的一些微服务使用log4j2作为记录器。Spring cloud Sleuth支持logback。在这种情况下,如何使用Sleuth获取分布式跟踪。我知道要在log4j2中使用侦探,我必须实现某些类。我试过了,但是没有成功。请帮帮忙
发布于 2020-11-08 17:51:30
Sleuth将traceId和spanId放在MDC(映射诊断上下文)中。
您可以使用%X检查MDC键值对,与Sleuth相关的键有traceId、spanId、parentId、spanExportable。
要模拟logback默认样式,只需将以下代码片段主要添加到您的PatternLayout中
[${APP_NAME},%X{traceId},%X{spanId},%X{spanExportable}]${APP_NAME}只是你的spring:application:name。
发布于 2018-02-14 03:38:09
请尝试使用最新的2.0.0.M6版本,我们在内部使用Brave。您可以查看https://github.com/openzipkin/brave/tree/master/context/log4j12模块如何正确设置日志记录机制。
在Spring Cloud Sleuth中,只需像这样创建一个bean:
@Bean
CurrentTraceContext log4jTraceContext() {
return MDCCurrentTraceContext.create();
}发布于 2018-10-13 14:45:56
这是我的示例log4j2配置,用于登录JSON,包括Sleuth的spanId和traceId。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="ConsoleJson" target="SYSTEM_OUT" follow="true">
<JsonLayout complete="false" compact="true" eventEol="true" properties="true"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleJson"/>
</Root>
</Loggers>
</Configuration>据the log4j2 Layouts documentation报道:
属性
布尔值
如果为true,则附加器将线程上下文映射包括在生成的JSON中。默认为false。
因此,线程上下文映射是此信息的实际持有者,要在其他布局(PatternLayout)中使用它,您必须使用一些上下文映射访问键,如%X{spanId}。
https://stackoverflow.com/questions/48774345
复制相似问题