Skip to content

Commit

Permalink
fixup context key collision (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign authored Dec 16, 2020
1 parent d433775 commit 13ed500
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package logger

import "context"

var contextKey struct{}
type contextKey int

const (
loggerKey contextKey = iota
)

// InjectInContext will inject a logger into the go-context
func InjectInContext(ctx context.Context, l LoggerInterface) context.Context {
return context.WithValue(ctx, contextKey, l)
return context.WithValue(ctx, loggerKey, l)
}

// FromContext will retrieve a logger from the go-context or return defaultLogger
func FromContext(ctx context.Context, defaultLogger LoggerInterface) LoggerInterface {
if _logger, ok := ctx.Value(contextKey).(LoggerInterface); ok {
if _logger, ok := ctx.Value(loggerKey).(LoggerInterface); ok {
return _logger
}
return defaultLogger
Expand Down

0 comments on commit 13ed500

Please sign in to comment.