1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00

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)`)
This commit is contained in:
Billy Lynch 2024-06-20 23:49:22 -04:00 committed by GitHub
parent 6962f14d44
commit 40901e8d69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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