From 6ec59ce7c1aa61225959320bbec94303db97e253 Mon Sep 17 00:00:00 2001 From: "Mario_Andre.Pinto_Ramos_Salgado" Date: Thu, 18 Jul 2024 16:07:20 +0200 Subject: [PATCH] fixup! go-template/_template/internal/log: swapped zap logger with log/slog; added trace context handler; added gRPC interceptor logger --- _template/internal/log/logger.go | 11 +++++++++++ _template/internal/log/logger_option.go | 10 ---------- 2 files changed, 11 insertions(+), 10 deletions(-) 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) {}