Skip to content

feat(api)!: log the default instance through slog.Default - #72

Merged
josuegiron merged 2 commits into
masterfrom
feat/default-instance-slog
Nov 21, 2026
Merged

josuegiron merged 2 commits into
masterfrom
feat/default-instance-slog

Conversation

@josuegiron

Copy link
Copy Markdown
Member

Scope

PR 2 of 2. It is stacked on #71 and targets feat/default-instance-wire. Merge #71 first, then retarget this PR to master. No tag.

This PR covers the logging half of the owner decision (2026-11-21): the default instance behaves like api.New(). After it lands, the default instance and instances share one code path, and no fromNew is left anywhere.

Changes

  • Logging: the default instance logs through slog.Default(), read at call time. It writes the same records as an instance with WithLogger: request, response, request body rejected, panic recovered, Get*Value* helper failures, and response cannot be encoded.
    • It never logs bodies or the form.
    • Sensitive keys are the built-in list plus SensitiveLogKeys, which can add keys but not remove one.
    • api.New() without WithLogger still logs nothing: New maps the nil logger to slog.DiscardHandler.
  • Writer: one ResponseWriter.Write. The zero writer logs through slog.Default and uses the registered formatter for its 500 body. The v0 writeEncodeFailure/writeEncoded are deleted.
  • Deprecations:
    • Newly deprecated: LogRequestBody, LogResponseBody, PrintFullEvent, MaxLoggedBodyBytes. They now affect only the deprecated Log* functions.
    • PrintError and Fatal are documented as ignored. Print is used only by the deprecated Log*.
    • TraceVisibility, TraceIDLength and BlankSuccess keep configuring the default instance and are not deprecated, because no option replaces them (constitution V).
  • Docs: the logging parts of README, the migration guide, docs/legacy.md and doc.go.

Breaking (default-instance users)

  • slog records go through slog.Default instead of Print/PrintError lines.
  • Assigning api.PrintError, api.Print or api.Fatal no longer routes the library's logs. Use slog.SetDefault or api.WithLogger.
  • Bodies are never logged, even with LogRequestBody set.
  • SensitiveLogKeys can only add keys.

Stay on v0.11.x to keep v0 behavior.

Tests

default_log_golden_test.go, the v0 lines, is replaced by golden slog records in default_instance_test.go (TestDefaultInstanceLogsThroughSlogDefault). New tests:

  • helper failures are recorded and their sensitive values are hidden;
  • SensitiveLogKeys only adds keys;
  • the encode-failure 500 and the bare 500;
  • New() stays silent under a capturing default logger;
  • Print and PrintError are never called.

The writer, recover and logging tests were moved off PrintError.

Size

1,000 changed lines (503+/497−), at the PR-02 limit.

Verification

  • gofmt -l . prints nothing.
  • go vet ./... passes.
  • go test -race ./... passes in every package.
  • govulncheck ./... finds no vulnerabilities.

go test -bench=. -run='^$' -benchmem ./..., before (master) → after:

Benchmark Before After
SuccessWrite/default 2820 ns, 24 allocs ~2500 ns, 21 allocs
WriteError/default 2780 ns, 23 allocs ~2500–2900 ns, 19 allocs
ProcessRequest/default 18387 ns, 130 allocs ~16300 ns, 105 allocs

The instance numbers are unchanged. The default instance now costs the same as an instance.

🤖 Generated with Claude Code

Second part of the owner decision (2026-11-21) that the default instance
behaves like api.New(). The default instance now writes the same slog
records as an instance with WithLogger, through slog.Default() read at
call time; an api.New() without WithLogger still logs nothing. The last
fromNew forks (logging, the response writer) are gone, so the default
instance and instances share one code path. The v0 golden log test is
replaced by golden slog records in default_instance_test.go.

Deprecated: LogRequestBody, LogResponseBody, PrintFullEvent and
MaxLoggedBodyBytes (they affect only the deprecated Log* functions).
PrintError and Fatal are documented as ignored. TraceVisibility,
TraceIDLength and BlankSuccess keep configuring the default instance and
stay undeprecated (no replacement option).

BREAKING CHANGE: for programs that use the package-level API without
api.New (the default instance):
- ProcessRequest, the body middlewares, the Get*Value* helpers and the
  response writer log slog records (request, response, request body
  rejected, panic recovered, ...) through slog.Default() instead of
  REQUEST/RESPONSE lines through Print and PrintError;
- the package no longer calls PrintError (or Fatal); Print is called only
  by the deprecated LogRequest/LogResponse*. A program that assigned
  api.PrintError or api.Print to route logs must call slog.SetDefault or
  move to api.New(api.WithLogger(...));
- request and response bodies and the parsed form are never logged,
  even with LogRequestBody or LogResponseBody set;
- SensitiveLogKeys adds keys to the built-in list and can no longer
  remove a built-in key;
- a type that embeds ResponseWriter logs through slog.Default.
Stay on v0.11.x to keep v0 behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@josuegiron
josuegiron changed the base branch from feat/default-instance-wire to master November 21, 2026 18:26
@josuegiron
josuegiron merged commit a1dae2c into master Nov 21, 2026
2 checks passed
@josuegiron
josuegiron deleted the feat/default-instance-slog branch November 21, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant