logging / test (push) Skipped
logging / vulnerabilities (push) Skipped
pre-commit / pre-commit (push) Skipped
logging / coverage-baseline (pull_request) Skipped
logging / test (pull_request) Successful in 42s
logging / vulnerabilities (pull_request) Successful in 29s
pre-commit / pre-commit (pull_request) Successful in 2m29s
The module is now git.unbound.se/shiny/logging. The API is unchanged. Tags up to v0.2.2 declare gitea.unbound.se/shiny/logging and stay resolvable through that alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kko4x3MYEcWMMdkx12WpBa
69 lines
2.2 KiB
Markdown
69 lines
2.2 KiB
Markdown
# logging
|
|
|
|
Shared Go library with logging primitives for all Shiny backend services.
|
|
|
|
## Shared Documentation
|
|
|
|
@../docs/claude/architecture.md
|
|
@../docs/claude/go-services.md
|
|
@../docs/claude/conventions.md
|
|
|
|
## Library Information
|
|
|
|
### Purpose
|
|
|
|
Single home for the `slog` setup, context-logger helpers, request-logging
|
|
middleware and the `MockLogger` test helper that were previously copied (with
|
|
drift) into every backend service.
|
|
|
|
### Usage
|
|
|
|
```go
|
|
import (
|
|
"git.unbound.se/shiny/logging"
|
|
logmw "git.unbound.se/shiny/logging/middleware"
|
|
)
|
|
|
|
// Configure the slog default logger (format: text | json | otel).
|
|
logger := logging.SetupLogger(cli.LogLevel, cli.LogFormat, serviceName, buildVersion)
|
|
|
|
// Carry a logger on the request context.
|
|
ctx = logging.ContextWithLogger(ctx, logger)
|
|
logger = logging.LoggerFromContext(ctx)
|
|
|
|
// Debug-log request/response bodies.
|
|
handler = logmw.RequestLogger(logger)(handler)
|
|
```
|
|
|
|
In tests:
|
|
|
|
```go
|
|
import logtest "git.unbound.se/shiny/logging/logtest"
|
|
|
|
m := logtest.NewMockLogger()
|
|
// ... exercise code with m.Logger() ...
|
|
m.Check(t, []string{"level=INFO msg=\"...\""})
|
|
```
|
|
|
|
### Exported API
|
|
|
|
- `SetupLogger(logLevel, logFormat, serviceName, buildVersion string) *slog.Logger`
|
|
- `ContextWithLogger(ctx, *slog.Logger) context.Context` / `LoggerFromContext(ctx) *slog.Logger`
|
|
- `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`.
|
|
|
|
### Notes
|
|
|
|
- `MockLogger`/`NewMockLogger` live in the `logging/logtest` sub-package, so the
|
|
production `logging` package's import graph is free of `testify` — consumers
|
|
only compile it if they import `logtest`. (`testify` stays in the module's
|
|
`go.mod` because `logtest` and the library's own tests use it.)
|
|
|
|
### Conventions
|
|
|
|
Standard Shiny library scaffolding: `gofumpt`/`goimports -local`, golangci-lint,
|
|
gitleaks and conventional-commit checks via pre-commit; coverage-regression gate
|
|
in CI (`.testcoverage.yml`); releases auto-tagged from conventional commits by
|
|
the shared Release workflow. Bump the consuming services' `go.mod` after a
|
|
release.
|