diff --git a/v2/logging.go b/v2/logging.go index 4e54c60b..766f62f6 100644 --- a/v2/logging.go +++ b/v2/logging.go @@ -12,8 +12,12 @@ func getLogger() *slog.Logger { return log } -func SetSlogHandler(handler slog.Handler) { - if handler != nil { +// EnableHandler allows to set a custom slog.Handler to be used by the go-shuttle logger. +// If handler is nil, the default slog logger will be used. +func EnableHandler(handler slog.Handler) { + if handler == nil { + log = slog.Default() + } else { log = slog.New(handler) } } diff --git a/v2/logging_test.go b/v2/logging_test.go index b343f1c4..10e8a30f 100644 --- a/v2/logging_test.go +++ b/v2/logging_test.go @@ -9,10 +9,11 @@ import ( ) func TestSetSlogHandler(t *testing.T) { - buf := &bytes.Buffer{} - SetSlogHandler(slog.NewTextHandler(buf, nil)) + g := NewWithT(t) + g.Expect(func() { EnableHandler(nil) }).ToNot(Panic()) + buf := &bytes.Buffer{} + EnableHandler(slog.NewTextHandler(buf, nil)) getLogger().Info("testInfo") - g := NewWithT(t) g.Expect(buf.String()).To(ContainSubstring("testInfo")) }