Skip to content

Commit

Permalink
fix: fix ilog.Logger interface for return logger (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Aug 24, 2023
2 parents dbe4166 + 53becb7 commit 7126f8b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
8 changes: 3 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package ilog

import "context"

type contextKey string

const contextKeyLogger contextKey = "Logger"
type contextKeyLogger struct{}

func FromContext(ctx context.Context) (logger Logger) {
if ctx == nil {
Global().Copy().AddCallerSkip(1).Errorf("ilog: nil context")
return Global().Copy()
}

v := ctx.Value(contextKeyLogger)
v := ctx.Value(contextKeyLogger{})
l, ok := v.(Logger)

if !ok {
Expand All @@ -24,5 +22,5 @@ func FromContext(ctx context.Context) (logger Logger) {
}

func WithContext(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, contextKeyLogger, logger)
return context.WithValue(ctx, contextKeyLogger{}, logger)
}
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestContext(t *testing.T) {
t.Run("failure,invalidType", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
defer SetGlobal(NewBuilder(DebugLevel, buf).SetTimestampZone(time.UTC).Build())()
_ = FromContext(context.WithValue(context.Background(), contextKeyLogger, "invalid")) //nolint:staticcheck
_ = FromContext(context.WithValue(context.Background(), contextKeyLogger{}, "invalid")) //nolint:staticcheck
if expected := regexp.MustCompilePOSIX(`{"severity":"ERROR","timestamp":"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.?[0-9]*Z","caller":"ilog\.go/[a-z_]+\.go:[0-9]+","message":"ilog: type assertion failed: expected=ilog.Logger, actual=string, value=\\"invalid\\""}`); !expected.Match(buf.Bytes()) {
t.Errorf("❌: !expected.Match(buf.Bytes()):\n%s", buf)
}
Expand Down
8 changes: 4 additions & 4 deletions ilog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type Logger interface {
// Level returns the current logging level of the logger.
Level() (currentLoggerLevel Level)
// SetLevel sets the logging level of the logger.
SetLevel(level Level) (logger Logger)
SetLevel(level Level) (copied Logger)
// AddCallerSkip adds the number of stack frames to skip to the logger.
AddCallerSkip(skip int) (logger Logger)
AddCallerSkip(skip int) (copied Logger)
// Copy returns a copy of the logger.
Copy() (copiedLogger Logger)
Copy() (copied Logger)

// Common is the interface that has the common logging methods for both ilog.Logger and ilog.LogEntry.
Common
Expand Down Expand Up @@ -84,7 +84,7 @@ type LogEntry interface {
Common

// Logger returns a new logger with the same fields of the log entry.
Logger() (copiedLogger Logger)
Logger() (copied Logger)

// error: for considering undispatched LogEntry as error so that they can be detected by Go static analysis.
error
Expand Down
10 changes: 6 additions & 4 deletions ilog_default_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ func (l *implLogger) Level() Level {
}

func (l *implLogger) SetLevel(level Level) Logger {
l.config.level = level
return l
copied := l.copy()
copied.config.level = level
return copied
}

func (l *implLogger) AddCallerSkip(skip int) Logger {
l.config.callerSkip += skip
return l
copied := l.copy()
copied.config.callerSkip += skip
return copied
}

func (l *implLogger) Copy() Logger {
Expand Down
10 changes: 6 additions & 4 deletions implementations/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func (l *implLogger) Level() ilog.Level {
}

func (l *implLogger) SetLevel(level ilog.Level) ilog.Logger {
l.level = level
return l
copied := l.copy()
copied.level = level
return copied
}

func (l *implLogger) AddCallerSkip(skip int) ilog.Logger {
l.zapLogger = l.zapLogger.WithOptions(zap.AddCallerSkip(skip))
return l
copied := l.copy()
copied.zapLogger = l.zapLogger.WithOptions(zap.AddCallerSkip(skip))
return copied
}

func (l *implLogger) Copy() ilog.Logger {
Expand Down
10 changes: 6 additions & 4 deletions implementations/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ func (l *implLogger) Level() ilog.Level {
}

func (l *implLogger) SetLevel(level ilog.Level) ilog.Logger {
l.level = level
return l
copied := l.copy()
copied.level = level
return copied
}

func (l *implLogger) AddCallerSkip(skip int) ilog.Logger {
copied := l.copy()
logger := l.zerologLogger.With().Caller().CallerWithSkipFrameCount(skip).Logger()
l.zerologLogger = &logger
return l
copied.zerologLogger = &logger
return copied
}

func (l *implLogger) Copy() ilog.Logger {
Expand Down

0 comments on commit 7126f8b

Please sign in to comment.