From 40901e8d693a6bc69ddd8252231eb9e128efd974 Mon Sep 17 00:00:00 2001 From: Billy Lynch <1844673+wlynch@users.noreply.github.com> Date: Thu, 20 Jun 2024 23:49:22 -0400 Subject: [PATCH] slogtest: Allow passing in logging config options. (#12) - Enables debug logging by default. - Adds `TestLoggerWithOptions` to allow overriding default config. - Adds `Context` as a shorthand for TestContextWithLogger (e.g. `slogtest.TestContextWithLogger(t)` -> `slogtest.Context(t)`) --- slogtest/slogtest.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/slogtest/slogtest.go b/slogtest/slogtest.go index c56bd72..939cb31 100644 --- a/slogtest/slogtest.go +++ b/slogtest/slogtest.go @@ -26,9 +26,23 @@ type Logger interface { Logf(format string, args ...any) } -// TestLogger gets a logger to use in unit and end to end tests +// TestLogger gets a logger to use in unit and end to end tests. +// This logger is configured to log at debug level. func TestLogger(t Logger) *clog.Logger { - return clog.New(slog.NewTextHandler(&logAdapter{l: t}, nil)) + return TestLoggerWithOptions(t, &slog.HandlerOptions{ + Level: slog.LevelDebug, + }) +} + +// TestLoggerWithOptions gets a logger to use in unit and end to end tests. +func TestLoggerWithOptions(t Logger, opts *slog.HandlerOptions) *clog.Logger { + return clog.New(slog.NewTextHandler(&logAdapter{l: t}, opts)) +} + +// Context returns a context with a logger to be used in tests. +// This is equivalent to TestContextWithLogger. +func Context(t Logger) context.Context { + return TestContextWithLogger(t) } // TestContextWithLogger returns a context with a logger to be used in tests