简体   繁体   English

如何在不使用 Sleuth 的情况下在 Spring Boot 中实现跟踪?

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

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.我正在尝试在登录我的 Spring Boot 应用程序期间实现 Tracing,Sleuth 是我实现的一种选择,而无需进行任何编码排序或其他操作。 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?我浏览了 sleuth 的源代码,发现他们正在修改日志记录级别模式以实现跟踪,如何在不添加 sleuth 依赖项的情况下修改日志记录模式?

1.Add dependency to project , 1.给项目添加依赖,

<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. 2.我有一个现有的logback-spring.xml & 在那里我不得不更改日志记录模式。 Without changing log pattern, ids were not showing in logs ,在不更改日志模式的情况下,日志中未显示 id,

Existing Pattern - %date %level [%thread] %logger{10} [%file:%line] %msg%n现有模式- %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新模式- %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.通过 slf4j,您可以通过这种方式以编程方式配置记录器,您不需要配置文件。

Your pom.xml must look like this:您的 pom.xml 必须如下所示:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot Spring Boot微服务中的Spring Cloud Sleuth日志跟踪 - Spring Cloud Sleuth log tracing in Spring Boot Microservice Spring 侦探 - JMS ErrorHandler 上的跟踪中断 - Spring Sleuth - broken tracing on JMS ErrorHandler 如何使用 Spring Boot JPA 和 PostgreSQL 对 DB 行中的加密值实现搜索功能 - How can I implement search facility on encrypted values in DB rows using Spring Boot JPA and PostgreSQL 如何在 Spring Boot 应用程序中实现 Firestore 时间戳 - How can I implement Firestore timestamp in Spring Boot Application 如何在Spring Boot中使用JWT身份验证实现基本身份验证? - How can I implement Basic Authentication with JWT authentication in Spring Boot? 如何在不使用if else的情况下实现Factory模式或在春季使用注释切换大小写 - How can i implement Factory pattern without using if else or switch case in spring using annotations 如何在不使用配置文件的情况下覆盖Spring Boot应用程序属性进行测试? - How can I override Spring Boot application properties for tests without using profiles? 如何使用Spring Boot实施“负载均衡器”? - How to implement “load balancer” using spring boot? 如何使用 Spring Boot 正确实现 @ForeignKey - How to implement @ForeignKey correctly using Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM