Skip to content

Commit a40a9d1

Browse files
committed
fix:修改获取logid和logger的context为接口类型
1 parent da3f3c2 commit a40a9d1

3 files changed

Lines changed: 28 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*.dll
88
*.so
99
*.dylib
10-
10+
*.log
1111
# Test binary, built with `go test -c`
1212
*.test
1313

pkg/logger/zapx/zapx.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"path/filepath"
1313
)
1414

15-
type ZapLogger struct{ l *zap.Logger }
15+
type ZapLogger struct {
16+
l *zap.Logger
17+
}
1618

1719
type ZapOption func(*ZapCfg)
1820

pkg/transport/http/ginx/log/log.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package log
22

33
import (
4+
"context"
45
"crypto/rand"
56
"crypto/sha1"
67
"encoding/hex"
@@ -11,10 +12,11 @@ import (
1112
)
1213

1314
const (
14-
LogIDKey = "Gin-LogID"
15-
LoggerKey = "Gin-Logger"
16-
NameKey = "Gin-Name"
17-
DefaultName = "unknown"
15+
LogIDKey = "Gin-LogID"
16+
LoggerKey = "Gin-Logger"
17+
NameKey = "Gin-Name"
18+
DefaultName = "unknown"
19+
DefaultLogID = "without logID"
1820
)
1921

2022
// 生成日志 ID
@@ -41,21 +43,13 @@ func SetLogID(ctx *gin.Context, logID string) {
4143
ctx.Set(LogIDKey, logID)
4244
}
4345

44-
// 设置到响应中需要
45-
func GetLogID(ctx *gin.Context) string {
46-
var logID string
47-
value, ok := ctx.Get(LogIDKey)
46+
// 为了保证获取的便利性这里用的是context.Context
47+
func GetLogID(ctx context.Context) string {
48+
value, ok := ctx.Value(LogIDKey).(string)
4849
if !ok {
49-
// 先尝试从X-Request-ID请求头里面取,如果需要用http跨服务调用的话可以考虑用这个保证整个调用链一致
50-
logID = ctx.Request.Header.Get("X-Request-ID")
51-
// 如果不存在则尝试去生成一个
52-
if logID == "" {
53-
logID = genLogID(GetGlobalName(ctx))
54-
}
55-
SetLogID(ctx, logID)
56-
return logID
50+
return DefaultLogID
5751
}
58-
return value.(string)
52+
return value
5953
}
6054

6155
// 用于设置在上下文中获取携带了特殊信息的日志,主动打印需要
@@ -64,18 +58,24 @@ func SetLogger(ctx *gin.Context, logger logger.Logger) {
6458
}
6559

6660
// 用于获取在上下文中获取携带了特殊信息的日志
67-
func GetLogger(ctx *gin.Context) logger.Logger {
68-
ginLogger, ok := ctx.Get(LoggerKey)
61+
func GetLogger(ctx context.Context) logger.Logger {
62+
ginLogger, ok := ctx.Value(LoggerKey).(logger.Logger)
6963
if !ok {
7064
return nil // 如果不存在需要返回一个空指针
7165
}
72-
return ginLogger.(logger.Logger)
66+
return ginLogger
7367
}
7468

7569
func GlobalLoggerMiddleware(l logger.Logger) gin.HandlerFunc {
7670
return func(ctx *gin.Context) {
71+
logID := ctx.Request.Header.Get("X-Request-ID")
72+
if logID == "" {
73+
logID = genLogID(GetGlobalName(ctx)) // 如果不存在则尝试去生成一个
74+
}
75+
76+
SetLogID(ctx, logID)
7777
l = l.With(logger.Field{
78-
"logID": GetLogID(ctx), // 保证ctx中的所有日志都是自带logID的
78+
"logID": logID, // 保证ctx中的所有日志都是自带logID的
7979
})
8080
SetLogger(ctx, l)
8181
}
@@ -87,10 +87,10 @@ func GlobalNameMiddleware(name string) gin.HandlerFunc {
8787
}
8888
}
8989

90-
func GetGlobalName(ctx *gin.Context) string {
91-
value, ok := ctx.Get(NameKey)
90+
func GetGlobalName(ctx context.Context) string {
91+
value, ok := ctx.Value(NameKey).(string)
9292
if !ok {
9393
return DefaultName
9494
}
95-
return value.(string)
95+
return value
9696
}

0 commit comments

Comments
 (0)