addDebugLogger method

void addDebugLogger({
  1. bool enabled = kDebugMode,
  2. bool printToConsole = kDebugMode,
  3. bool redactSensitiveHeaders = true,
  4. int maxConsoleBodyLength = 2000,
  5. List<DebugEnvironment> environments = const [],
})

Records every request of this Dio instance in the debug logger.

final dio = Dio()..addDebugLogger();

Does nothing when enabled is false (defaults to false in release builds) or when the logger is already attached.

Pass environments to switch this Dio between backends from the log page. The Dio's own baseUrl should be one of them; requests to other hosts are never changed.

dio.addDebugLogger(environments: [
  DebugEnvironment('Dev', baseUrl: 'https://dev.api.example.com'),
  DebugEnvironment('Prod', baseUrl: 'https://api.example.com'),
]);

Implementation

void addDebugLogger({
  bool enabled = kDebugMode,
  bool printToConsole = kDebugMode,
  bool redactSensitiveHeaders = true,
  int maxConsoleBodyLength = 2000,
  List<DebugEnvironment> environments = const [],
}) {
  if (!enabled) return;
  if (interceptors.any((interceptor) => interceptor is DebugLogging)) return;
  assert(() {
    final base = options.baseUrl;
    String norm(String u) =>
        u.endsWith('/') ? u.substring(0, u.length - 1) : u;
    final list = environments.isNotEmpty
        ? environments
        : DebugEnvironments.instance.global;
    if (list.isNotEmpty &&
        base.isNotEmpty &&
        !list.any((e) => norm(e.baseUrl) == norm(base))) {
      debugPrint(
        'dio_debug_logger: this Dio\'s baseUrl "$base" is not one of its '
        'environments, so switching environments will not affect it.',
      );
    }
    return true;
  }());
  interceptors.add(
    DebugLogging(
      enabled: true,
      client: this,
      printToConsole: printToConsole,
      redactSensitiveHeaders: redactSensitiveHeaders,
      maxConsoleBodyLength: maxConsoleBodyLength,
      environments: environments,
    ),
  );
}