feat: initial shared logging module
slog SetupLogger (text/json/otel), context logger helpers, MockLogger test helper, and a request-logger HTTP middleware sub-package. Replaces the logging package + middleware request-logger copied across the backend services.
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSetupLogger(t *testing.T) {
|
||||
for _, format := range []string{"text", "json"} {
|
||||
l := SetupLogger("info", format, "test-service", "v0.0.0")
|
||||
assert.NotNil(t, l)
|
||||
assert.Same(t, l, slog.Default())
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextLogger(t *testing.T) {
|
||||
base := SetupLogger("info", "text", "svc", "v1")
|
||||
assert.Same(t, base, LoggerFromContext(context.Background()))
|
||||
custom := slog.New(slog.NewTextHandler(nil, nil))
|
||||
ctx := ContextWithLogger(context.Background(), custom)
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user