How can I implement Tracing in Spring Boot without using Sleuth?

Viewed 675

I'm trying to implement Tracing during logging in my Spring Boot application, Sleuth is one option for me implement without doing any coding sort or thing. But my problem is with customization. I went through the source code of sleuth and found out that they're modifying the logging level pattern to implement tracing, how can I modify the logging pattern without adding the sleuth dependency?

2 Answers

1.Add dependency to project ,

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
             <version>1.3.3.RELEASE</version>
</dependency>

2.I had an existing logback-spring.xml & there I had to change logging pattern. Without changing log pattern, ids were not showing in logs ,

Existing Pattern - %date %level [%thread] %logger{10} [%file:%line] %msg%n

New Pattern- %date %level [%thread] %logger{10} [%file:%line] [%X{X-B3-TraceId},%X{X-B3-SpanId},%X{X-B3-ParentSpanId},%X{X-Span-Export}] %msg%n

You can use somethin like this:

package com.team.monitoring;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;

public final class MetricRegistrySingleton {

    public static final MetricRegistry metrics = new MetricRegistry();

    static {
        Logger logger = LoggerFactory.getLogger("com.team.monitoring");
        final Slf4jReporter reporter = Slf4jReporter.forRegistry(metrics).outputTo(logger).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
        reporter.start(5, TimeUnit.MINUTES);
    }

    private MetricRegistrySingleton() {
        throw new AssertionError();
    }

}

In this way by slf4j you can configure the logger programatically, you dont need configuration files.

Your pom.xml must look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javacodegeeks</groupId>
<artifactId>slf4-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>slf4-spring-boot</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.5.3.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <version>1.5.3.RELEASE</version>
    </dependency>
</dependencies>
Related