1 Star 0 Fork 7

小桥流水 / tracelog

forked from 乌云 / tracelog 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

traclog 日志追踪

介绍

这是一个日志追踪的组件,适用于spring为架构项目中。原理是利用过滤器或拦截器拦截请求,生成一个uuid,并加入到logback的上下文MDC中,然后在logback.xml中打印出来,使日志调用链具有可检索的全局唯一id。

日志追踪的工具有很多,但是要么都很重,安装使用成本很高,要么不全面,不能同时兼容多种框架,traclog很轻很简单,特别适合小型单机项目或者业务不太复杂的微服务,大型项目除了日志追踪可能还要日志收集和链路追踪,那么请参考其他开源工具,比如skywalking:https://www.jianshu.com/p/b69bc629f476

使用

只能在java springboot框架的项目中使用,并且日志组件用的使logback。支持以下常见应用生成或传递traceId:springmvc的http, springcloud的feign,有注解@Scheduled,@PostConstruct,@Async的方法,apache dubbo调用,以及支持Okhttp和HttpClient调用时传递traceId(需要自己将已实现的拦截器加入到你的Okhttp和HttpClient)。

  1. 引入依赖
    建议www.search.maven.org搜最新版本
<dependency>
    <groupId>com.wuyunonline.tracelog</groupId>
    <artifactId>tracelog-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
  1. 配置logback
    项目中找到logback.xml或logback-spring.xml, 在日志格式中加入[traceId:%X{traceId}] 例如: 图一

  2. 启动项目
    启动自己的项目后,测试一下发起一个请求,可以看到日志打印traceId说明成功,例如: 图二

扩展

  1. 这里涉及到的dubbo必须是apache dubbo 依赖是org.apache.dubbo这样的,其他dubbo可能不兼容(老版的是阿里的,阿里已将dubbo加入apache)。
  2. 使用Okhttp或HttpClient调用时候如果想传递traceId,必须在自己的Okhttp或HttpClient中加入traclog 拦截器,如下:
    Okhttp
import com.wuyunonline.tracelog.interceptor.TraceLogOkhttpInterceptor;
...
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new TraceLogOkhttpInterceptor()).build();

HttpClient

import com.wuyunonline.tracelog.interceptor.TraceLogHttpClientInterceptor;
...
CloseableHttpClient httpClient = HttpClientBuilder.create().addInterceptorFirst(new TraceLogHttpClientInterceptor()).build();
  1. 由于精力有限,未能全面测试,如果引入jar没有效果,可能是某些原因导致自动注入未生效,这时候最快的方式是自实现,请参考文档:
    https://www.jianshu.com/p/c93e55b1c011
MIT License Copyright (c) 2022 乌云 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

日志追踪:为logback 日志添加全局唯一标识traceId,实现追踪日志链 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/studyshao/tracelog.git
git@gitee.com:studyshao/tracelog.git
studyshao
tracelog
tracelog
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891