Skip to content

Commit

Permalink
add default slog logger
Browse files Browse the repository at this point in the history
  • Loading branch information
karenychen committed Apr 26, 2024
1 parent 07070b0 commit 87fc63f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions v2/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 4 additions & 3 deletions v2/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

0 comments on commit 87fc63f

Please sign in to comment.