优化tace_id

This commit is contained in:
Yun
2024-07-03 16:58:14 +08:00
parent 3ebbf617de
commit d41516d05d
2 changed files with 18 additions and 2 deletions
+5
View File
@@ -87,6 +87,11 @@ func (l *Logger) Channel(ch string) (r *Logger) {
return &rr
}
// 获取TraceField的字段
func (l *Logger) GetTraceField() string {
return l.option.traceField
}
// 实现io.Writer接口
func (l *Logger) Write(b []byte) (n int, err error) {
return l.write("info", b)
+13 -2
View File
@@ -5,10 +5,11 @@ import (
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"github.com/yuninks/loggerx"
)
// 设置普通的traceId
func SetTraceId(ctx context.Context, traceKey string) context.Context {
func SetTraceIdByKey(ctx context.Context, traceKey string) context.Context {
if traceKey == "" {
traceKey = "trace_id"
}
@@ -20,8 +21,13 @@ func SetTraceId(ctx context.Context, traceKey string) context.Context {
return ctx
}
// 设置logger的traceId
func SetTraceId(ctx context.Context, logger *loggerx.Logger) context.Context {
return SetTraceIdByKey(ctx, logger.GetTraceField())
}
// 设置Gin的traceId
func SetGinTraceId(traceKey string) gin.HandlerFunc {
func SetGinTraceIdByKey(traceKey string) gin.HandlerFunc {
if traceKey == "" {
traceKey = "trace_id"
@@ -35,3 +41,8 @@ func SetGinTraceId(traceKey string) gin.HandlerFunc {
ctx.Set(traceKey, traceId)
}
}
// 设置Gin的traceId
func SetGinTraceIdByLogger(logger *loggerx.Logger) gin.HandlerFunc {
return SetGinTraceIdByKey(logger.GetTraceField())
}