Skip to content

Commit

Permalink
refactor: rename logger.Logger to logger.Provider for better naming
Browse files Browse the repository at this point in the history
Signed-off-by: lvlcn-t <[email protected]>
  • Loading branch information
lvlcn-t committed Sep 30, 2024
1 parent 2ff7a7f commit d234b98
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions examples/extension/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}),
Expand Down
14 changes: 7 additions & 7 deletions internal/logger/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)}
}

Expand Down
42 changes: 21 additions & 21 deletions internal/logger/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
12 changes: 6 additions & 6 deletions internal/logger/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)),
}
Expand All @@ -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),
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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()
}
Expand Down
10 changes: 5 additions & 5 deletions internal/logger/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -148,7 +148,7 @@ func TestFromContext(t *testing.T) {
tests := []struct {
name string
ctx context.Context
want Logger
want Provider
}{
{
name: "Context with logger",
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestFromSlog(t *testing.T) {
tests := []struct {
name string
l *slog.Logger
want Logger
want Provider
}{
{
name: "Slog logger",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit d234b98

Please sign in to comment.