From ab6de721a62154f033acb8c81ea34ca676f7ef04 Mon Sep 17 00:00:00 2001
From: dtapps
Date: Wed, 27 Mar 2024 10:49:43 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E8=B0=83=E6=95=B4=20`slog`?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
const.go | 6 ------
context.go | 51 +++----------------------------------------------
context_test.go | 3 +++
version.go | 2 +-
5 files changed, 8 insertions(+), 56 deletions(-)
diff --git a/README.md b/README.md
index 2b1676d..566158d 100644
--- a/README.md
+++ b/README.md
@@ -13,5 +13,5 @@
#### 安装
```shell
-go get -v -u go.dtapp.net/gotrace_id@v1.0.10
+go get -v -u go.dtapp.net/gotrace_id@v1.0.11
```
diff --git a/const.go b/const.go
index c9cf221..517d182 100644
--- a/const.go
+++ b/const.go
@@ -2,12 +2,6 @@ package gotrace_id
const Nil = "%!s()"
-type ctxKey string
-
-const (
- SlogFields ctxKey = "slog_fields"
-)
-
const (
TraceIdKey = "trace_id"
TraceIdGinKey = "trace_id"
diff --git a/context.go b/context.go
index d3da8a7..629277c 100644
--- a/context.go
+++ b/context.go
@@ -5,68 +5,23 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"go.dtapp.net/gostring"
- "log/slog"
)
// CustomTraceIdContext 自定义设置跟踪编号上下文
func CustomTraceIdContext(ctx context.Context) context.Context {
-
- if ctx == nil {
- ctx = context.Background()
- }
-
var traceId = gostring.GetUuId()
-
- // slog
- if v, ok := ctx.Value(SlogFields).([]slog.Attr); ok {
- v = append(v, slog.String(TraceIdKey, traceId))
- return context.WithValue(ctx, SlogFields, v)
- }
-
- var v []slog.Attr
- v = append(v, slog.String(TraceIdKey, traceId))
-
- return context.WithValue(context.WithValue(ctx, SlogFields, v), TraceIdKey, traceId)
+ return context.WithValue(ctx, TraceIdKey, traceId)
}
// SetCustomTraceId 自定义设置跟踪编号上下文
func SetCustomTraceId(ctx context.Context, traceId string) context.Context {
-
- if ctx == nil {
- ctx = context.Background()
- }
-
- // slog
- if v, ok := ctx.Value(SlogFields).([]slog.Attr); ok {
- v = append(v, slog.String(TraceIdKey, traceId))
- return context.WithValue(ctx, SlogFields, v)
- }
-
- var v []slog.Attr
- v = append(v, slog.String(TraceIdKey, traceId))
-
- return context.WithValue(context.WithValue(ctx, SlogFields, v), TraceIdKey, traceId)
+ return context.WithValue(ctx, TraceIdKey, traceId)
}
// SetGinTraceIdContext 设置跟踪编号上下文
func SetGinTraceIdContext(ctx context.Context, c *gin.Context) context.Context {
-
- if ctx == nil {
- ctx = context.Background()
- }
-
var traceId = GetGinTraceId(c)
-
- // slog
- if v, ok := ctx.Value(SlogFields).([]slog.Attr); ok {
- v = append(v, slog.String(TraceIdKey, traceId))
- return context.WithValue(ctx, SlogFields, v)
- }
-
- var v []slog.Attr
- v = append(v, slog.String(TraceIdKey, traceId))
-
- return context.WithValue(context.WithValue(ctx, SlogFields, v), TraceIdKey, traceId)
+ return context.WithValue(ctx, TraceIdKey, traceId)
}
// GetTraceIdContext 通过上下文获取跟踪编号
diff --git a/context_test.go b/context_test.go
index 630b147..83cdbcb 100644
--- a/context_test.go
+++ b/context_test.go
@@ -12,4 +12,7 @@ func TestContext(t *testing.T) {
ctx2 := context.Background()
t.Log(ctx2)
t.Log(GetTraceIdContext(ctx2))
+ ctx3 := SetCustomTraceId(context.Background(), "23456")
+ t.Log(ctx3)
+ t.Log(GetTraceIdContext(ctx3))
}
diff --git a/version.go b/version.go
index 3634803..177fa63 100644
--- a/version.go
+++ b/version.go
@@ -1,3 +1,3 @@
package gotrace_id
-const Version = "1.0.10"
+const Version = "1.0.11"