LoggingInterceptor class

The default configuration is conservative (method, URL, status — no headers, no bodies) so it is safe to drop in production without exposing secrets. Headers and bodies are opt-in via the includeHeaders and includeBodies flags.

When bodies are included, the body text is passed through redactBody before being logged. The default redactor is redactPragmaKey (added in 1.0.5), which replaces the literal value of any PRAGMA key = '...' or PRAGMA rekey = '...' statement with '***'. This means a SQLCipher database password attached to a request body (e.g. inside a RestRequest.body that contains a PRAGMA key line) is never written to the log, even when body logging is enabled.

Pass redactBody: (s) => s (an identity function) to disable redaction. The default is redactPragmaKey, so most callers never need to think about it.

Example

dRest.use(LoggingInterceptor(
  log: (line) => developer.log(line, name: 'rest'),
));

// Verbose (with bodies, redacted by default):
dRest.use(LoggingInterceptor(
  log: print,
  includeBodies: true,
));

// Custom redactor (e.g. for an OAuth bearer token):
dRest.use(LoggingInterceptor(
  log: print,
  includeBodies: true,
  redactBody: (sql) => sql.replaceAll(
    RegExp(r'''Bearer [A-Za-z0-9._-]+'''),
    'Bearer ***',
  ),
));

The interceptor does not measure elapsed time: RestRequest is immutable, so a stopwatch would have nowhere to live. If you need per-call latency, wrap the call in your own Stopwatch before invoking the codegen-emitted client method.

Implemented types

Constructors

LoggingInterceptor({required void log(String), bool includeBodies = false, bool includeHeaders = false, String redactBody(String)?})
Creates an interceptor that writes one log line per request, response, and error to log.

Properties

hashCode int
The hash code for this object.
no setterinherited
includeBodies bool
Whether to append the request/response body to the log line. The body is passed through redactBody first. Default false.
final
includeHeaders bool
Whether to append the request/response headers to the log line. Default false. Headers may contain auth tokens — enable with care.
final
log → void Function(String)
Where each line is written. The line is a single String with no trailing newline; the sink decides how to terminate it. Typical sinks: print, developer.log, or a custom ZoneSpecification#print handler.
final
redactBody String Function(String)?
Function that scrubs a logged body string before it is written. Default: redactPragmaKey. null disables redaction.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onError(RestException error) Future<RestException>
Called when the request fails (network error or non-success status code).
override
onRequest(RestRequest request) Future<RestRequest>
Called before the request is sent. Returns the (possibly modified) request, or throws a RestException to abort the call.
override
onResponse(RestResponse response) Future<RestResponse>
Called after the response is received (including error responses).
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited