From d234b98803b70578b06d5ec362ae1d90377d2927 Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Tue, 1 Oct 2024 00:17:02 +0200 Subject: [PATCH] refactor: rename logger.Logger to logger.Provider for better naming Signed-off-by: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> --- examples/extension/main.go | 6 ++--- internal/logger/core.go | 14 +++++----- internal/logger/extensions_test.go | 42 +++++++++++++++--------------- internal/logger/utils.go | 12 ++++----- internal/logger/utils_test.go | 10 +++---- logger/logger.go | 18 ++++++------- 6 files changed, 51 insertions(+), 51 deletions(-) diff --git a/examples/extension/main.go b/examples/extension/main.go index 66d4dd8..ef61d3a 100644 --- a/examples/extension/main.go +++ b/examples/extension/main.go @@ -9,14 +9,14 @@ import ( ) type Logger interface { - logger.Logger + logger.Provider Success(msg string, args ...any) } const LevelSuccess = slog.Level(1) type loggerExtension struct { - logger.Logger + logger.Provider } // Success logs at LevelSuccess. @@ -31,7 +31,7 @@ func (l *loggerExtension) Success(msg string, args ...any) { func NewLogger() Logger { return &loggerExtension{ - Logger: logger.NewLogger(logger.Options{ + Provider: logger.NewLogger(logger.Options{ Handler: slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ ReplaceAttr: replaceAttr, }), diff --git a/internal/logger/core.go b/internal/logger/core.go index 4e6397d..c693418 100644 --- a/internal/logger/core.go +++ b/internal/logger/core.go @@ -7,10 +7,10 @@ import ( "time" ) -var _ Logger = (*logger)(nil) +var _ Provider = (*logger)(nil) -// Logger is a interface that provides logging methods. -type Logger interface { +// Provider is a interface that provides logging methods. +type Provider interface { // Trace logs at [LevelTrace]. Trace(msg string, args ...any) // Tracef logs at [LevelTrace]. @@ -69,14 +69,14 @@ type Logger interface { FatalContext(ctx context.Context, msg string, args ...any) // With returns a Logger that has the given attributes. - With(args ...any) Logger + With(args ...any) Provider // WithGroup returns a Logger that starts a group, if name is non-empty. // The keys of all attributes added to the Logger will be qualified by the given // name. (How that qualification happens depends on the [Handler.WithGroup] // method of the Logger's Handler.) // // If name is empty, WithGroup returns the receiver. - WithGroup(name string) Logger + WithGroup(name string) Provider // Log emits a log record with the current time and the given level and message. // The Record's Attrs consist of the Logger's attributes followed by @@ -146,12 +146,12 @@ func (l *logger) ErrorContext(ctx context.Context, msg string, a ...any) { } // With calls Logger.With on the default logger. -func (l *logger) With(a ...any) Logger { +func (l *logger) With(a ...any) Provider { return &logger{Logger: l.Logger.With(a...)} } // WithGroup returns a Logger that starts a group, if name is non-empty. -func (l *logger) WithGroup(name string) Logger { +func (l *logger) WithGroup(name string) Provider { return &logger{Logger: l.Logger.WithGroup(name)} } diff --git a/internal/logger/extensions_test.go b/internal/logger/extensions_test.go index 0d5c443..7073089 100644 --- a/internal/logger/extensions_test.go +++ b/internal/logger/extensions_test.go @@ -8,7 +8,7 @@ import ( "github.com/lvlcn-t/loggerhead/internal/logger/test" ) -type logFunc func(l Logger, msg string, args ...any) +type logFunc func(l Provider, msg string, args ...any) func TestLogger_LevelExtensions(t *testing.T) { tests := []struct { @@ -18,7 +18,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }{ { name: "debug level disabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Debugf(msg, args...) }, handler: test.MockHandler{ @@ -33,7 +33,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "debug level enabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Debugf(msg, args...) }, handler: test.MockHandler{ @@ -51,7 +51,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "info level disabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Infof(msg, args...) }, handler: test.MockHandler{ @@ -66,7 +66,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "info level enabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Infof(msg, args...) }, handler: test.MockHandler{ @@ -84,7 +84,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "warn level disabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Warnf(msg, args...) }, handler: test.MockHandler{ @@ -99,7 +99,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "warn level enabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Warnf(msg, args...) }, handler: test.MockHandler{ @@ -117,7 +117,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "error level disabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Errorf(msg, args...) }, handler: test.MockHandler{ @@ -132,7 +132,7 @@ func TestLogger_LevelExtensions(t *testing.T) { }, { name: "error level enabled", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Errorf(msg, args...) }, handler: test.MockHandler{ @@ -168,7 +168,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "trace level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Trace(msg, args...) }, handler: test.MockHandler{ @@ -179,7 +179,7 @@ func TestLogger_CustomLevels(t *testing.T) { }, { name: "tracef level", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Tracef(msg, args...) }, handler: test.MockHandler{ @@ -191,7 +191,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "trace context level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.TraceContext(context.Background(), msg, args...) }, handler: test.MockHandler{ @@ -203,7 +203,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "notice level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Notice(msg, args...) }, handler: test.MockHandler{ @@ -214,7 +214,7 @@ func TestLogger_CustomLevels(t *testing.T) { }, { name: "noticef level", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Noticef(msg, args...) }, handler: test.MockHandler{ @@ -226,7 +226,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "notice context level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.NoticeContext(context.Background(), msg, args...) }, handler: test.MockHandler{ @@ -238,7 +238,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "panic level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Panic(msg, args...) }, handler: test.MockHandler{ @@ -250,7 +250,7 @@ func TestLogger_CustomLevels(t *testing.T) { }, { name: "panicf level", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Panicf(msg, args...) }, handler: test.MockHandler{ @@ -263,7 +263,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "panic context level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.PanicContext(context.Background(), msg, args...) }, handler: test.MockHandler{ @@ -276,7 +276,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "fatal level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Fatal(msg, args...) }, handler: test.MockHandler{ @@ -288,7 +288,7 @@ func TestLogger_CustomLevels(t *testing.T) { }, { name: "fatalf level", - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.Fatalf(msg, args...) }, handler: test.MockHandler{ @@ -301,7 +301,7 @@ func TestLogger_CustomLevels(t *testing.T) { { name: "fatal context level", attrs: []any{"key", "value"}, - logFunc: func(l Logger, msg string, args ...any) { + logFunc: func(l Provider, msg string, args ...any) { l.FatalContext(context.Background(), msg, args...) }, handler: test.MockHandler{ diff --git a/internal/logger/utils.go b/internal/logger/utils.go index 5854c74..5b99595 100644 --- a/internal/logger/utils.go +++ b/internal/logger/utils.go @@ -23,7 +23,7 @@ import ( // opts := logger.Options{Level: "INFO", Format: "JSON", OpenTelemetry: true} // log := logger.NewLogger(opts) // log.Info("Hello, world!") -func NewLogger(o ...Options) Logger { +func NewLogger(o ...Options) Provider { return &logger{ Logger: slog.New(newHandler(o...)), } @@ -36,7 +36,7 @@ func NewLogger(o ...Options) Logger { // // opts := logger.Options{Level: "DEBUG", Format: "TEXT"} // log := logger.NewNamedLogger("myServiceLogger", opts) -func NewNamedLogger(name string, o ...Options) Logger { +func NewNamedLogger(name string, o ...Options) Provider { return &logger{ Logger: slog.New(newHandler(o...)).With("name", name), } @@ -56,16 +56,16 @@ type ctxKey struct{} // IntoContext embeds the provided slog.Logger into the given context and returns the modified context. // This function is used for passing loggers through context, allowing for context-aware logging. -func IntoContext(ctx context.Context, log Logger) context.Context { +func IntoContext(ctx context.Context, log Provider) context.Context { return context.WithValue(ctx, ctxKey{}, log) } // FromContext extracts the slog.Logger from the provided context. // If the context does not have a logger, it returns a new logger with the default configuration. // This function is useful for retrieving loggers from context in different parts of an application. -func FromContext(ctx context.Context) Logger { +func FromContext(ctx context.Context) Provider { if ctx != nil { - if logger, ok := ctx.Value(ctxKey{}).(Logger); ok { + if logger, ok := ctx.Value(ctxKey{}).(Provider); ok { return logger } } @@ -93,7 +93,7 @@ func (l *logger) ToSlog() *slog.Logger { } // FromSlog returns a new Logger instance based on the provided slog.Logger. -func FromSlog(l *slog.Logger) Logger { +func FromSlog(l *slog.Logger) Provider { if l == nil { return NewLogger() } diff --git a/internal/logger/utils_test.go b/internal/logger/utils_test.go index 7a5de7c..13bbb85 100644 --- a/internal/logger/utils_test.go +++ b/internal/logger/utils_test.go @@ -134,7 +134,7 @@ func TestNewContextWithLogger(t *testing.T) { defer cancel() log := ctx.Value(ctxKey{}) - if _, ok := log.(Logger); !ok { + if _, ok := log.(Provider); !ok { t.Errorf("Context does not contain Logger, got %T", log) } if ctx == tt.parentCtx { @@ -148,7 +148,7 @@ func TestFromContext(t *testing.T) { tests := []struct { name string ctx context.Context - want Logger + want Provider }{ { name: "Context with logger", @@ -182,7 +182,7 @@ func TestFromSlog(t *testing.T) { tests := []struct { name string l *slog.Logger - want Logger + want Provider }{ { name: "Slog logger", @@ -213,7 +213,7 @@ func TestFromSlog(t *testing.T) { func TestLogger_ToSlog(t *testing.T) { tests := []struct { name string - l Logger + l Provider }{ { name: "Logger", @@ -259,7 +259,7 @@ func TestMiddleware(t *testing.T) { t.Run(tt.name, func(t *testing.T) { middleware := Middleware(tt.parentCtx) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, ok := r.Context().Value(ctxKey{}).(Logger) + _, ok := r.Context().Value(ctxKey{}).(Provider) if tt.expectInCtx != ok { t.Errorf("Middleware() did not inject logger correctly, got %v, want %v", ok, tt.expectInCtx) } diff --git a/logger/logger.go b/logger/logger.go index b99f237..a344c1b 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -8,9 +8,9 @@ import ( "github.com/lvlcn-t/loggerhead/internal/logger" ) -// Logger is the interface for the logger. +// Provider is the interface for the logger. // Its build on top of slog.Logger and extends it with additional logging methods. -type Logger = logger.Logger +type Provider = logger.Provider // Options is the optional configuration for the logger. type Options = logger.Options @@ -25,7 +25,7 @@ type Options = logger.Options // opts := logger.Options{Level: "INFO", Format: "JSON", OpenTelemetry: true} // log := logger.NewLogger(opts) // log.Info("Hello, world!") -func NewLogger(o ...logger.Options) logger.Logger { +func NewLogger(o ...logger.Options) logger.Provider { return logger.NewLogger(o...) } @@ -36,7 +36,7 @@ func NewLogger(o ...logger.Options) logger.Logger { // // opts := logger.Options{Level: "DEBUG", Format: "TEXT"} // log := logger.NewNamedLogger("myServiceLogger", opts) -func NewNamedLogger(name string, o ...logger.Options) logger.Logger { +func NewNamedLogger(name string, o ...logger.Options) logger.Provider { return logger.NewNamedLogger(name, o...) } @@ -50,16 +50,16 @@ func NewContextWithLogger(parent context.Context) (context.Context, context.Canc return logger.NewContextWithLogger(parent) } -// IntoContext embeds the provided [logger.Logger] into the given context and returns the modified context. +// IntoContext embeds the provided [logger.Provider] into the given context and returns the modified context. // This function is used for passing loggers through context, allowing for context-aware logging. -func IntoContext(ctx context.Context, log logger.Logger) context.Context { +func IntoContext(ctx context.Context, log logger.Provider) context.Context { return logger.IntoContext(ctx, log) } -// FromContext extracts the [logger.Logger] from the provided context. +// FromContext extracts the [logger.Provider] from the provided context. // If the context does not have a logger, it returns a new logger with the default configuration. // This function is useful for retrieving loggers from context in different parts of an application. -func FromContext(ctx context.Context) logger.Logger { +func FromContext(ctx context.Context) logger.Provider { return logger.FromContext(ctx) } @@ -69,6 +69,6 @@ func Middleware(ctx context.Context) func(http.Handler) http.Handler { } // FromSlog returns a new [Logger] instance from the provided [slog.Logger]. -func FromSlog(l *slog.Logger) logger.Logger { +func FromSlog(l *slog.Logger) logger.Provider { return logger.FromSlog(l) }