feat: move MockLogger into a logtest sub-package (#5)
logging / test (push) Has been skipped
logging / vulnerabilities (push) Has been skipped
Release / release (push) Successful in 56s
logging / coverage-baseline (push) Successful in 2m49s
pre-commit / pre-commit (push) Successful in 5m29s

This commit was merged in pull request #5.
This commit is contained in:
2026-06-19 16:43:26 +00:00
parent e067a72887
commit a705fc33c4
4 changed files with 23 additions and 16 deletions
+8 -7
View File
@@ -38,7 +38,9 @@ handler = logmw.RequestLogger(logger)(handler)
In tests: In tests:
```go ```go
m := logging.NewMockLogger() import logtest "gitea.unbound.se/shiny/logging/logtest"
m := logtest.NewMockLogger()
// ... exercise code with m.Logger() ... // ... exercise code with m.Logger() ...
m.Check(t, []string{"level=INFO msg=\"...\""}) m.Check(t, []string{"level=INFO msg=\"...\""})
``` ```
@@ -47,16 +49,15 @@ m.Check(t, []string{"level=INFO msg=\"...\""})
- `SetupLogger(logLevel, logFormat, serviceName, buildVersion string) *slog.Logger` - `SetupLogger(logLevel, logFormat, serviceName, buildVersion string) *slog.Logger`
- `ContextWithLogger(ctx, *slog.Logger) context.Context` / `LoggerFromContext(ctx) *slog.Logger` - `ContextWithLogger(ctx, *slog.Logger) context.Context` / `LoggerFromContext(ctx) *slog.Logger`
- `Logger` interface; `NewMockLogger() *MockLogger` (+ `MockLogger.Logger()`, `MockLogger.Check(t, want)`). - `logging/logtest.NewMockLogger() *MockLogger` (+ `MockLogger.Logger()`, `MockLogger.Check(t, want)`) — the test helper, in its own sub-package so production code doesn't pull in testify.
- `logging/middleware.RequestLogger(logger) func(http.Handler) http.Handler`. - `logging/middleware.RequestLogger(logger) func(http.Handler) http.Handler`.
### Notes ### Notes
- `MockLogger` currently lives in the main package, so `testify` is a non-test - `MockLogger`/`NewMockLogger` live in the `logging/logtest` sub-package, so the
dependency of the module. Moving it to a `logging/logtest` sub-package is a production `logging` package's import graph is free of `testify` — consumers
tracked low-priority follow-up — it's a breaking import change for the ~13 only compile it if they import `logtest`. (`testify` stays in the module's
services that reference `logging.NewMockLogger`, so it is deferred until a `go.mod` because `logtest` and the library's own tests use it.)
coordinated bump (Ambix 019ecabc).
### Conventions ### Conventions
-8
View File
@@ -23,11 +23,3 @@ func TestContextLogger(t *testing.T) {
ctx := ContextWithLogger(context.Background(), custom) ctx := ContextWithLogger(context.Background(), custom)
assert.Same(t, custom, LoggerFromContext(ctx)) assert.Same(t, custom, LoggerFromContext(ctx))
} }
func TestMockLogger(t *testing.T) {
m := NewMockLogger()
m.Logger().Info("hello", "k", "v")
m.Check(t, []string{`level=INFO msg=hello k=v`})
empty := NewMockLogger()
empty.Check(t, nil)
}
+4 -1
View File
@@ -1,4 +1,7 @@
package logging // Package logtest provides test helpers for the logging library. It is kept in
// its own package so that importing the production logging package does not pull
// in testify (a test-only dependency) — only consumers that import logtest do.
package logtest
import ( import (
"bytes" "bytes"
+11
View File
@@ -0,0 +1,11 @@
package logtest
import "testing"
func TestMockLogger(t *testing.T) {
m := NewMockLogger()
m.Logger().Info("hello", "k", "v")
m.Check(t, []string{`level=INFO msg=hello k=v`})
empty := NewMockLogger()
empty.Check(t, nil)
}