Skip to main content

Dio HTTP logging

Use DioLoggingInterceptor to emit structured Log Events for HTTP requests, responses and errors. It delegates to a pre-configured StructureLogger (and its sinks such as SinkSeq).

Setup

import 'package:dio/dio.dart';
import 'package:structured_logger/structured_logger.dart';
import 'package:structured_logger_dio_interceptor/structured_logger_dio_interceptor.dart';

final logger = StructureLogger()
..addSink(SinkSeq(
'https://your-seq.example.com',
apiKey: 'your-api-key',
deviceIdentifier: 'my-app',
));

final dio = Dio()..interceptors.add(DioLoggingInterceptor(logger));

Behavior

  • onRequest: generates correlational UUID (default header X-Request-Seq-Id), sets X-Request-Start-Time, emits REQUEST event.
  • onResponse: emits RESPONSE with statusCode, elapsedTime (ms), headers, data.
  • onError: emits ON_ERROR (level error) with optional status and error data.

All events go through your registered sinks — no direct HTTP from the interceptor.

Constructor

DioLoggingInterceptor(
this._logger, {
this.correlationalHeaderName = 'X-Request-Seq-Id',
})
  • correlationalHeaderName must be non-empty (asserts otherwise).
  • deviceIdentifier lives on SinkSeq, not here.

Templates (example)

The emitted @mt values follow legacy semantics for easy querying:

  • REQUEST: REQUEST: {@method} {@path} ...
  • RESPONSE / ERROR analogous.

See the package source for exact templates and data keys (event_type, elapsedTime, etc.).