Skip to content

Commit

Permalink
refactor: use wrapper functions instead of variable assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Feb 24, 2024
1 parent 3d6d55f commit f068096
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package logger

import (
"context"
"log/slog"
"net/http"

"github.com/lvlcn-t/loggerhead/internal/logger"
)

// Logger is the interface for the logger.
// Its build on top of slog.Logger and extends it with additional logging methods.
type Logger = logger.Logger

// Level is the type for log levels.
type Level logger.Level

// NewLogger creates a new Logger instance.
// If handlers are provided, the first handler in the slice is used; otherwise,
// a default JSON handler writing to os.Stderr is used. This function allows for
Expand All @@ -17,28 +24,40 @@ type Logger = logger.Logger
//
// log := logger.NewLogger()
// log.Info("Hello, world!")
var NewLogger = logger.NewLogger
func NewLogger(h ...slog.Handler) logger.Logger {
return logger.NewLogger(h...)
}

// NewNamedLogger creates a new Logger instance with the provided name.
// If handlers are provided, the first handler in the slice is used; otherwise,
// a default JSON handler writing to os.Stderr is used. This function allows for
// custom configuration of logging handlers.
var NewNamedLogger = logger.NewNamedLogger
func NewNamedLogger(name string, h ...slog.Handler) logger.Logger {
return logger.NewNamedLogger(name, h...)
}

// NewContextWithLogger creates a new context based on the provided parent context.
// It embeds a logger into this new context, which is a child of the logger from the parent context.
// The child logger inherits settings from the parent.
// Returns the child context and its cancel function to cancel the new context.
var NewContextWithLogger = logger.NewContextWithLogger
func NewContextWithLogger(parent context.Context) (context.Context, context.CancelFunc) {
return logger.NewContextWithLogger(parent)
}

// 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.
var IntoContext = logger.IntoContext
func IntoContext(ctx context.Context, log logger.Logger) context.Context {
return logger.IntoContext(ctx, 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.
var FromContext = logger.FromContext
func FromContext(ctx context.Context) logger.Logger {
return logger.FromContext(ctx)
}

// Middleware takes the logger from the context and adds it to the request context.
var Middleware = logger.Middleware
func Middleware(ctx context.Context) func(http.Handler) http.Handler {
return logger.Middleware(ctx)
}

0 comments on commit f068096

Please sign in to comment.