导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
日志
<configuration>
<!-- TraceId:%X{X-B3-TraceId:-} SpanId:%X{X-B3-SpanId:-}-->
<property name="CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [traceId= %X{X-B3-TraceId:-}] [SpanId= %X{X-B3-SpanId:-}] %logger{5} - %msg%n"/>
<appender name="rollingAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/billmanager.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/logs/heuristic-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<!-- 必须指定,否则不会往文件输出内容 -->
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
<append>false</append>
<prudent>false</prudent>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="rollingAppender"/>
</root>
</configuration>
获取当前id
import org.springframework.cloud.sleuth.Tracer;
@Service
public class Breadcrumb {
@Autowired
private Tracer tracer;
public String breadcrumbId() {
return tracer.getCurrentSpan().traceIdString();
}
}