Skip to content

feat(http-backend): add injectable structured request logging hook - #370

Closed
melanindebbie wants to merge 1 commit into
Vellar-Wallet:devfrom
melanindebbie:feat/247-structured-error-logging
Closed

feat(http-backend): add injectable structured request logging hook#370
melanindebbie wants to merge 1 commit into
Vellar-Wallet:devfrom
melanindebbie:feat/247-structured-error-logging

Conversation

@melanindebbie

@melanindebbie melanindebbie commented Aug 31, 2026

Copy link
Copy Markdown

Closes #247

Summary

Adds an optional structured logging hook to createHttpWalletBackend so consumer log pipelines can parse, filter, and route on structured fields (method, url, status, durationMs) instead of free-form console output.

What changed

  • src/http-backend.ts — exported RequestLog interface, RequestLogHook type, and added an optional third logHook parameter to createHttpWalletBackend. The internal post helper now measures round-trip duration with performance.now() and invokes the hook once per completed request (success and failure alike).
  • src/http-backend.test.ts — new test file with 7 tests covering the logging hook on every endpoint and edge case (see below).
  • README.md — added a "Logging" section with a usage example and updated the Helpers table to document the log? parameter.

Key design decisions

  1. Injectable hook, not console output — the hook is opt-in (third arg, defaults to undefined). No opinionated logger dependency, no console.* calls. Consumers wire their own structured logger.
  2. Fires on success AND failure — the hook is called after fetchImpl returns, before error-throwing logic, so a 500 or 503 is logged the same way as a 200.
  3. No try/catch around hook — a broken hook will propagate. This matches the existing fetchImpl contract and keeps the behavior predictable. Consumer hooks should be fast and fail-safe.
  4. performance.now() for timing — browser/Node native, sub-millisecond precision, no extra dependencies.

Acceptance criteria checklist

# Criterion Satisfied by
1 Injectable structured logging hook logHook?: RequestLogHook third param on createHttpWalletBackend
2 Method, url, status, duration in each log call RequestLog interface has all four fields; hook is called in post() with values from the actual request
3 Test verifying hook receives expected fields 7 tests in http-backend.test.ts: success paths (create, submit), failure paths (create 500, connect 401, submit 503), edge case (404 returns undefined), and no-hook no-op
4 README updated with logging hook interface "Logging" section with code example + Helpers table updated

Test output

✓ src/http-backend.test.ts (7 tests) 15ms

All 521 tests pass. Typecheck (tsc --noEmit) is clean. No lint issues.

Follow-ups

  • None required. The hook interface is deliberately minimal. If consumers need request/response body logging, they can extend the hook type downstream.

Security note

No secrets, keys, or sensitive data are included in log entries. The hook receives only the HTTP method, full URL (which may include a base URL the consumer controls), status code, and duration. No request or response bodies are logged.

createHttpWalletBackend now accepts an optional logHook that is invoked once
per completed request with { method, url, status, durationMs }, so consumers
can parse, filter, and route on structured fields instead of free-form output.
Backwards-compatible: omitting the hook leaves logging disabled. Adds a test
suite covering success, failure, the 404 lookup edge case, and the no-hook
path, and documents the new interface in the README.
@github-actions
github-actions Bot changed the base branch from main to dev August 31, 2026 08:09
@github-actions

Copy link
Copy Markdown

Hi @melanindebbie — thanks for the contribution!

We do not accept pull requests to the main branch. All contributions go to the dev branch, so I've automatically retargeted this PR from main to dev for you.

You don't need to reopen anything — your work is preserved and this PR now targets dev. Going forward, please set the base branch to dev when you open a PR. 🙏

@github-actions

Copy link
Copy Markdown

Thanks for the contribution — but contributor PRs may only change files inside contrib/.

This PR touches file(s) outside that folder:

  • README.md
  • src/http-backend.test.ts
  • src/http-backend.ts

Please open a new PR with your changes scoped to contrib/ only, targeting dev. See CONTRIBUTING.md and contrib/README.md. If your assigned issue genuinely needs changes elsewhere, say so on the issue first — don't open a PR outside contrib/.

Questions? Ask in the Telegram group.

@github-actions github-actions Bot closed this Aug 31, 2026
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.

[easy] Add structured error logging hook to http-backend.ts

1 participant