无法在Spring Cloud Sleuth中观察TraceId和SpanId

f5emj3cl  于 2022-12-13  发布在  Spring
关注(0)|答案(2)|浏览(344)

我正在尝试运行Spring Cloud Sleuth并观察日志中的traceid、span id。
我已经配置如下,但当我调用一个requesti cnat看到任何traceId或logId在日志中。
有人能帮我吗。谢谢。

2020-12-02 11:40:02 [main] INFO  az.iba.ms.chasis.ChasisApplication - Started ChasisApplication in 21.425 seconds (JVM running for 23.816)
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.a.c.c.C.[.[localhost].[/chasis-ms] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 24 ms
2020-12-02 11:40:17 [http-nio-8081-exec-1] INFO  a.i.m.c.controller.ChasisController - Request {}helloChasis from chasis-ms

build.gradle

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.2.6.RELEASE'

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '2.2.6.RELEASE'

Controller.java

package az.iba.ms.chasis.controller;

import az.iba.ms.chasis.entity.Chasis;
import az.iba.ms.chasis.logger.MainLogger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping(value = "/v1")
@Log4j2
@Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = "Chasis microservice")
public class ChasisController {

    private static final MainLogger LOGGER = MainLogger.getLogger(ChasisController.class);

    private static final Logger LOG = LoggerFactory.getLogger(ChasisController.class);

    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value = "View a list of accounts for given CIF list", response = Chasis.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved message"),
            @ApiResponse(code = 404, message = "The resource is not found")}
    )
    @GetMapping("/hello")
    public String helloChasis() {
        LOG.info("Request {}" + "helloChasis from chasis-ms");
        return "Greetings from Chasis";
    }

}
g6ll5ycj

g6ll5ycj1#

如果我需要猜测的话,这是由您的一些自定义Log4J设置引起的,更准确地说是由您的Log4J模式引起的(我没有看到它们,所以我只是猜测)。logging config)的数据。
我建议你给予一下,首先使用默认的配置,如果它工作,你可以自定义默认的模式(我不建议自定义它,默认是相当不错的)。

628mspwn

628mspwn2#

您将需要添加配置以让日志提供程序知道您要记录这些附加字段。示例logback.xml应如下所示:-

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} traceId: %X{traceId} spanId: %X{spanId} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
    <appender-ref ref="STDOUT" />
    </root>
</configuration>

Spring-cloud sleuth会在您正在使用的日志提供程序的MDC中自动添加属性traceId和spanId

相关问题