Skip to content

Commit

Permalink
internal/log: refactored to use an empty struct as the context key
Browse files Browse the repository at this point in the history
  • Loading branch information
azazeal committed Oct 1, 2021
1 parent 0f89434 commit 564fdb7
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ import (
"github.com/azazeal/flycast/internal/common"
)

type contextKeyType int

const contextKey contextKeyType = iota + 1
type contextKeyType struct{}

// FromContext returns the Logger the given Context carries.
//
// FromContext panics in case the given Context carries no Logger.
func FromContext(ctx context.Context) *zap.Logger {
return ctx.Value(contextKey).(*zap.Logger)
return ctx.Value(contextKeyType{}).(*zap.Logger)
}

// NewContext returns a copy of ctx which carries logger.
func NewContext(ctx context.Context, logger *zap.Logger) context.Context {
return context.WithValue(ctx, contextKey, logger)
return context.WithValue(ctx, contextKeyType{}, logger)
}

const stampLayout = "2006-01-02 15:04:05.000"
Expand Down

0 comments on commit 564fdb7

Please sign in to comment.