Skip to content

Commit

Permalink
修复获取 context logger 失败
Browse files Browse the repository at this point in the history
  • Loading branch information
feyman committed Jan 16, 2024
1 parent 64e55cd commit 0eabf07
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 62 deletions.
32 changes: 18 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ const (
TraceIDKeyName CtxKey = "trace_id"
)

//
// CtxLogger

Check warning on line 24 in context.go

View workflow job for this annotation

GitHub Actions / Test on go 1.18 and ubuntu-latest

comment on exported function CtxLogger should be of the form "CtxLogger ..."

Check warning on line 24 in context.go

View workflow job for this annotation

GitHub Actions / Test on go 1.18 and ubuntu-latest

comment on exported function CtxLogger should be of the form "CtxLogger ..."
// @Description: get the ctxLogger in context
// @param c
// @param fields
// @return *zap.Logger
//
// @Description: get the ctxLogger in context
// @param c
// @param fields
// @return *zap.Logger
func CtxLogger(c context.Context, fields ...zap.Field) *zap.Logger {
if c == nil {
c = context.Background()
Expand Down Expand Up @@ -81,15 +80,14 @@ func CtxTraceID(c context.Context) string {
return xid.New().String()
}

//
// NewCtxLogger

Check warning on line 83 in context.go

View workflow job for this annotation

GitHub Actions / Test on go 1.18 and ubuntu-latest

comment on exported function NewCtxLogger should be of the form "NewCtxLogger ..."

Check warning on line 83 in context.go

View workflow job for this annotation

GitHub Actions / Test on go 1.18 and ubuntu-latest

comment on exported function NewCtxLogger should be of the form "NewCtxLogger ..."
// @Description: return a context with baseLogger and trace id and a baseLogger with trace id
// @param c
// @param logger
// @param traceID
// @return context.Context
// @return *zap.Logger
//
// @Description: return a context with baseLogger and trace id and a baseLogger with trace id
// @param c
// @param logger
// @param traceID
// @return context.Context
// @return *zap.Logger
func NewCtxLogger(c context.Context, logger *zap.Logger, traceID string) (context.Context, *zap.Logger) {
if c == nil {
c = context.Background()
Expand All @@ -99,10 +97,16 @@ func NewCtxLogger(c context.Context, logger *zap.Logger, traceID string) (contex
}
ctxLogger := logger.With(zap.String(string(TraceIDKeyName), traceID))
if gc, ok := c.(*gin.Context); ok {
// set ctxlogger in gin.Context
gc.Set(string(CtxLoggerName), ctxLogger)
// set traceID in gin.Context
gc.Set(string(TraceIDKeyName), traceID)
} else {
// set ctxlogger in context.Context
c = context.WithValue(c, CtxLoggerName, ctxLogger)
// set traceID in context.Context
c = context.WithValue(c, TraceIDKeyName, traceID)
}
// set traceID in context.Context
c = context.WithValue(c, TraceIDKeyName, traceID)

return c, ctxLogger
}
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ require (
github.com/go-redis/redis/v8 v8.11.5
github.com/json-iterator/go v1.1.12
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/prometheus/client_golang v1.14.0
github.com/rs/xid v1.4.0
go.uber.org/zap v1.24.0
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.24.6
)

require (
github.com/antlabs/strsim v0.0.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
Expand All @@ -28,7 +27,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand All @@ -37,17 +36,13 @@ require (
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/speps/go-hashids v2.0.0+incompatible // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
Expand All @@ -69,5 +64,4 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.4.4 // indirect
)
Loading

0 comments on commit 0eabf07

Please sign in to comment.