Skip to content

Commit fc62a5b

Browse files
committed
set client ip in gin context
1 parent 4ad4bab commit fc62a5b

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func CtxLogger(c context.Context, fields ...zap.Field) *zap.Logger {
3131
var ctxLoggerItf interface{}
3232
if gc, ok := c.(*gin.Context); ok {
3333
ctxLoggerItf, _ = gc.Get(string(CtxLoggerName))
34+
// 默认添加client ip
35+
if len(fields) == 0 {
36+
fields = append(fields, zap.String("ClientIP", gc.ClientIP()))
37+
}
3438
} else {
3539
ctxLoggerItf = c.Value(CtxLoggerName)
3640
}

0 commit comments

Comments
 (0)