From f068096479a5ded24b3cd76c8b85bd1814f458ca Mon Sep 17 00:00:00 2001 From: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com> Date: Sat, 24 Feb 2024 15:59:09 +0100 Subject: [PATCH] refactor: use wrapper functions instead of variable assignments --- logger/logger.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 0fef180..585c75c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,6 +1,10 @@ package logger import ( + "context" + "log/slog" + "net/http" + "github.com/lvlcn-t/loggerhead/internal/logger" ) @@ -8,6 +12,9 @@ import ( // 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 @@ -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) +}