Fix staticcheck's SA1029

This commit is contained in:
Deluan 2020-05-13 16:49:55 -04:00
parent 5ce3135f00
commit f8362a4acb
19 changed files with 152 additions and 57 deletions

View file

@ -24,6 +24,10 @@ const (
LevelTrace = Level(logrus.TraceLevel)
)
type contextKey string
const loggerCtxKey = contextKey("logger")
var (
currentLevel Level
defaultLogger = logrus.New()
@ -66,7 +70,7 @@ func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Conte
}
logger := addFields(createNewLogger(), keyValuePairs)
ctx = context.WithValue(ctx, "logger", logger)
ctx = context.WithValue(ctx, loggerCtxKey, logger)
return ctx
}
@ -176,7 +180,7 @@ func extractLogger(ctx interface{}) (*logrus.Entry, error) {
case *logrus.Entry:
return ctx, nil
case context.Context:
logger := ctx.Value("logger")
logger := ctx.Value(loggerCtxKey)
if logger != nil {
return logger.(*logrus.Entry), nil
}

View file

@ -136,7 +136,7 @@ var _ = Describe("Logger", func() {
It("returns the logger from context if it has one", func() {
logger := logrus.NewEntry(logrus.New())
ctx := context.Background()
ctx = context.WithValue(ctx, "logger", logger)
ctx = context.WithValue(ctx, loggerCtxKey, logger)
Expect(extractLogger(ctx)).To(Equal(logger))
})
@ -144,7 +144,7 @@ var _ = Describe("Logger", func() {
It("returns the logger from request's context if it has one", func() {
logger := logrus.NewEntry(logrus.New())
ctx := context.Background()
ctx = context.WithValue(ctx, "logger", logger)
ctx = context.WithValue(ctx, loggerCtxKey, logger)
req := httptest.NewRequest("get", "/", nil).WithContext(ctx)
Expect(extractLogger(req)).To(Equal(logger))