Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add traceId support #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

wuma2020
Copy link
Member

@wuma2020 wuma2020 commented Aug 2, 2022

  1. 开启打印traceId日志配置:在 application.properties 文件中新加logger.open-trace-id=true配置,即可开启traceId的日志打印
  2. traceId 的生成:使用 org.slf4j.impl.MDC的put(String key, String val)方法,完成traceId设置,配合1中的配置,日志会打印出该线程上下文的traceId
  3. 结合blade的使用,即在Blade.create().before(..)中,使用 org.slf4j.impl包下的 MDC.put("traceId", UUID.randomUUID().toString()),即可

下面是测试代码和日志

com.blade.log.LoggerTest#testMDCLog

`
2022/08/02 21:31:49 INFO [ pool-1-thread-2 ] c.b.l.LoggerTest : Hello World
2022/08/02 21:31:49 INFO [ pool-1-thread-1 ] c.b.l.LoggerTest : Hello World
2022/08/02 21:31:49 INFO [ Thread-2 ] [traceId: e9fe8921-6adb-48db-89a5-c155eeb4f04a] c.b.l.LoggerTest : Thread ,test traceId
2022/08/02 21:31:49 INFO [ Thread-3 ] [traceId: f964bec2-3733-4af1-aa23-9f34cb0daf24] c.b.l.LoggerTest : Thread ,test traceId
2022/08/02 21:31:49 INFO [ pool-2-thread-1 ] [traceId: e9fe8921-6adb-48db-89a5-c155eeb4f04a] c.b.l.LoggerTest : newFixedThreadPool,test traceId
2022/08/02 21:31:49 INFO [ pool-3-thread-1 ] [traceId: f964bec2-3733-4af1-aa23-9f34cb0daf24] c.b.l.LoggerTest : newFixedThreadPool,test traceId
2022/08/02 21:31:49 INFO [ ForkJoinPool.commonPool-worker-5 ] [traceId: e9fe8921-6adb-48db-89a5-c155eeb4f04a] c.b.l.LoggerTest : CompletableFuture ,test traceId
2022/08/02 21:31:49 INFO [ ForkJoinPool.commonPool-worker-3 ] [traceId: f964bec2-3733-4af1-aa23-9f34cb0daf24] c.b.l.LoggerTest : CompletableFuture ,test traceId
2022/08/02 21:31:54 INFO [ pool-1-thread-2 ] c.b.l.LoggerTest : Hello World
2022/08/02 21:31:54 INFO [ pool-1-thread-1 ] c.b.l.LoggerTest : Hello World
over

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant