diff --git a/_template/internal/log/logger.go b/_template/internal/log/logger.go index 46b810e2..a464beb8 100644 --- a/_template/internal/log/logger.go +++ b/_template/internal/log/logger.go @@ -50,6 +50,17 @@ func New(opts ...Option) *slog.Logger { return logger } +// FromHandler initializes a logger with the provided slog.Handler interface. +// +// If the provided handler is nil, the function returns a no-op slog.Logger. +func FromHandler(handler slog.Handler) *slog.Logger { + if handler == nil { + return NoOp() + } + + return slog.New(NewSpanContextHandler(handler, true)) +} + func NoOp() *slog.Logger { return slog.New(noOpHandler{}) } diff --git a/_template/internal/log/logger_option.go b/_template/internal/log/logger_option.go index e31e82a6..c757d885 100644 --- a/_template/internal/log/logger_option.go +++ b/_template/internal/log/logger_option.go @@ -22,16 +22,6 @@ func (o OptionFunc) apply(c *Config) { o(c) } -func WithHandler(handler slog.Handler) OptionFunc { - if handler == nil { - return func(*Config) {} - } - - return func(config *Config) { - config.handler = handler - } -} - func WithWriter(writer io.Writer) OptionFunc { if writer == nil { return func(*Config) {}