DEV Community

Jeff
Jeff

Posted on Originally published at powerduck.com

Codegen Custom Logging Middleware

Quick answer: A custom logging middleware wraps the HTTP layer of a generated client and records a structured entry around every call, including the method, path, status, latency, and correlation id, while keeping secrets and personal data out of the log. The generator gives you the interceptor point where the middleware runs, but the exact fields, redaction rules, log levels, and output destination are configuration you provide. Placing logging in one middleware means every operation, including retries and failures, is observed the same way instead of each call site deciding what to record.

What the middleware records

The useful entry is a structured object rather than a built string, so fields can be queried and redacted by name. Record the operation name, HTTP method, URL path, status code, elapsed time, and a correlation id that ties the call to the rest of a trace. When the client sits behind a retry interceptor, the entry should also capture the attempt number, otherwise a request that failed twice and then succeeded looks like a single clean call.

What must stay out

The same boundary that writes the log is the right place to redact, not a cleanup pass after the fact. The Authorization header, bearer tokens, API keys, and any sensitive fields in the request or response body have to be masked or omitted before the entry is serialized. Working on a structured object lets rules target known keys precisely, while pattern matching covers secrets in free text. Logging the full body by default is the common way this goes wrong; body logging should be opt-in and filtered.

Logging failures and retries

A middleware that only logs completed responses misses the cases you most need to debug. Network errors, timeouts, and aborted requests have to produce an entry with the error type, and each retry attempt should be logged as it happens rather than collapsed into the final result.

Event What to record
Success Status, latency, correlation id
Retry Attempt number, reason, delay
4xx or 5xx Status, operation, level
Network failure Error type, no assumed status

Levels and volume

Not every entry deserves the same level, and a flat stream hides the events that matter. Network failures and 5xx responses fit the error level, 4xx responses fit a warning, and successful calls are usually debug-level detail rather than a default info line on every request. Levels let a production deployment raise the threshold to cut noise and storage cost without changing the middleware or redeploying the client. Logging every successful call at info is a quiet cost: under high throughput it produces a large volume of low-value entries that bury the failures and push up the price of the log store.

Verifying the logs

The middleware still needs a check against real traffic. Run an ordered scenario that mixes a success, a failed call, and a retried call against a local mock, then compare the entries with the requests the mock actually recorded; Powerduck can drive that scenario and confirm the attempt count and status match, and that the secret fields were masked rather than merely omitted from view.

Honest limits

The generator does not know your log schema or where logs are shipped, so the destination and retention policy are yours. Automatic redaction can miss new formats, nested values, or encoded secrets, and it can also flag non-sensitive fields by mistake. The client controls only its own logs; the server, the upstream, and any log aggregation service keep their own records and access rules, and a log store without access controls becomes a second place for secrets to leak.

Frequently asked questions

Should the middleware log request bodies? Keep it off by default, and filter sensitive fields when body logging is turned on.

How should retries appear? As separate entries with the attempt number and reason, not one collapsed result.

When does redaction happen? At the write boundary, before the entry is serialized, rather than in a later cleanup.

Does the generator configure log output? It provides the hook, but the format, levels, and destination are your configuration.

The bottom line

A logging middleware gives every generated call a structured, queryable record and treats retries and failures as first-class events, while secrets are masked at the write boundary. Verify the entries against real scenarios and treat the log store itself as sensitive. For more on generated clients, see the OpenAPI codegen guide hub.

Top comments (0)