Skip to content

Commit

Permalink
Don't panic if there is no logger in the context. (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Aug 29, 2024
1 parent b392b65 commit 3d4201b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logger

import (
"context"
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -12,8 +11,6 @@ type logFiedType int

const logField logFiedType = iota

var mu sync.Mutex

// EncoderConfig is the config of log encoder.
var EncoderConfig = zapcore.EncoderConfig{
TimeKey: "ts",
Expand Down Expand Up @@ -58,10 +55,11 @@ func With(ctx context.Context, fields ...zap.Field) context.Context {

// Get gets logger from context.
func Get(ctx context.Context) *zap.Logger {
mu.Lock()
defer mu.Unlock()

return ctx.Value(logField).(*zap.Logger)
log := ctx.Value(logField)
if log == nil {
return nil
}
return log.(*zap.Logger)
}

// WithLogger adds existing logger to context.
Expand Down

0 comments on commit 3d4201b

Please sign in to comment.