dio_ansi_logger

pub version License: MIT Dart

A beautiful, Postman-style Dio interceptor that logs HTTP requests and responses with ANSI colors, structured formatting, and fully customizable themes — straight to your Flutter debug console.


✨ Features

  • 📦 Structured output — bordered sections for REQUEST, RESPONSE ✓ and ERROR ✕
  • 🎨 5 built-in themesdark, minimal, solarized, nord, matrix
  • 🖌️ Fully customizable — control the color of every single field
  • 🌈 Syntax-highlighted JSON — keys, strings, numbers, booleans and nulls each get their own color
  • Response time tracking — see exactly how long every request takes
  • 🔒 Header redaction — sensitive headers masked automatically
  • 🔍 Smart filtering — silence logs for specific endpoints
  • 🧩 Granular logging control — toggle headers/body independently
  • 📝 Custom log output — send logs to Talker, Firebase, or anywhere
  • ✂️ Body truncation — prevent huge payloads flooding the console
  • 🎯 Improved FormData support — readable fields + file metadata
  • 🔢 Scientific number support — handles 1e10, 2.3E-4
  • 🌐 Extended HTTP methods — HEAD, OPTIONS, CONNECT, TRACE
  • 🔗 Underlined URLs — better readability in logs
  • 🌀 cURL export — print a ready-to-run curl command after each request
  • ♻️ Retry logging — surface retry attempts from your retry interceptor
  • 0️⃣ Zero extra dependencies — only requires dio

📸 Output Preview

╔════════════════════════════════════════════════════
║  REQUEST
╠════════════════════════════════════════════════════
  Method  : POST
  URL     : https://api.example.com/auth/login
  Headers :
             content-type: application/json
             authorization: [REDACTED]
  Body    :
    {
      "email": "rafi@example.com",
      "password": "secret"
    }
┌─ cURL ─────────────────────────────────
curl -X POST 'https://api.example.com/auth/login' \
  -H 'content-type: application/json' \
  -H 'authorization: [REDACTED]' \
  -d '{"email":"rafi@example.com","password":"secret"}'
└────────────────────────────────────────
╚════════════════════════════════════════════════════

╔════════════════════════════════════════════════════
║  RESPONSE ✓
╠════════════════════════════════════════════════════
  Status  : 200 OK
  Method  : POST
  URL     : https://api.example.com/auth/login
  Time    : ⏱ 213 ms
  Body    :
    {
      "token": "eyJhbGciOiJIUzI1NiJ9...",
      "expiresIn": 3600
    }
╚════════════════════════════════════════════════════

Colors render in the VS Code Debug Console and Unix terminals.
Android Studio users: install the ANSI Highlighting plugin.


🚀 Installation

Add to your pubspec.yaml:

dependencies:
  dio_ansi_logger: ^1.2.0

Then run:

flutter pub get

🔧 Usage

import 'package:dio/dio.dart';
import 'package:dio_ansi_logger/dio_ansi_logger.dart';

final dio = Dio();
dio.interceptors.add(const DioLogger());

That's it. Every request, response and error will now be logged beautifully.


🎨 Switching Themes

// Dark terminal — Postman-inspired (default)
dio.interceptors.add(const DioLogger());
dio.interceptors.add(const DioLogger(theme: LoggerThemes.dark));

// Other built-in themes
dio.interceptors.add(const DioLogger(theme: LoggerThemes.minimal));
dio.interceptors.add(const DioLogger(theme: LoggerThemes.solarized));
dio.interceptors.add(const DioLogger(theme: LoggerThemes.nord));
dio.interceptors.add(const DioLogger(theme: LoggerThemes.matrix)); // 🟢 bright green

🖌️ Custom Theme

Use LoggerTheme with Ansi constants to build your own palette:

import 'package:dio_ansi_logger/dio_ansi_logger.dart';

const myTheme = LoggerTheme(
  sectionBorder:   Ansi.dim + Ansi.magenta,
  sectionTitle:    Ansi.bold + Ansi.brightMagenta,
  label:           Ansi.dim + Ansi.white,
  value:           Ansi.brightWhite,
  methodGet:       Ansi.bold + Ansi.brightGreen,
  methodPost:      Ansi.bold + Ansi.brightBlue,
  methodPut:       Ansi.bold + Ansi.brightYellow,
  methodDelete:    Ansi.bold + Ansi.brightRed,
  methodPatch:     Ansi.bold + Ansi.brightMagenta,
  statusSuccess:   Ansi.bold + Ansi.brightGreen,
  statusRedirect:  Ansi.bold + Ansi.brightYellow,
  statusError:     Ansi.bold + Ansi.brightRed,
  jsonKey:         Ansi.brightCyan,
  jsonString:      Ansi.brightGreen,
  jsonNumber:      Ansi.brightYellow,
  jsonBool:        Ansi.brightMagenta,
  jsonNull:        Ansi.dim + Ansi.white,
  errorTitle:      Ansi.bold + Ansi.brightRed,
  errorValue:      Ansi.red,
  dim:             Ansi.dim + Ansi.white,
  reset:           Ansi.reset,   // ← always keep this as Ansi.reset
);

dio.interceptors.add(const DioLogger(theme: myTheme));

Ansi color reference

Code Color Code Color
Ansi.red Red Ansi.brightRed Bright Red
Ansi.green Green Ansi.brightGreen Bright Green
Ansi.yellow Yellow Ansi.brightYellow Bright Yellow
Ansi.blue Blue Ansi.brightBlue Bright Blue
Ansi.magenta Magenta Ansi.brightMagenta Bright Magenta
Ansi.cyan Cyan Ansi.brightCyan Bright Cyan
Ansi.white White Ansi.brightWhite Bright White
Ansi.bold Bold Ansi.dim Dim

Combine them: Ansi.bold + Ansi.brightGreen = bold bright green.


⚙️ Options

Parameter Type Default Description
theme LoggerTheme LoggerThemes.dark Color theme
logRequest bool true Log outgoing requests
logResponse bool true Log successful responses
logError bool true Log errors (debug mode only)
maxBodyLength int 5000 Max body chars before truncation
logResponseTime bool true Show ⏱ elapsed ms on response/error
redactedHeaders Set<String> {authorization, x-api-key, cookie, set-cookie} Headers to mask
redactedPlaceholder String [REDACTED] Value shown instead of redacted header
showCurl bool false Print a curl command after each request
logRetries bool false Print ♻ Retry #N on each retry attempt

🔒 Header Redaction

Sensitive headers are masked by default. You can customise which headers are redacted:

dio.interceptors.add(const DioLogger(
  // Add to or replace the default set
  redactedHeaders: {'authorization', 'x-api-key', 'cookie', 'x-refresh-token'},
  redactedPlaceholder: '••••••',   // optional — defaults to [REDACTED]
));

Matching is case-insensitiveAuthorization and authorization are both caught.

To disable redaction entirely, pass an empty set:

dio.interceptors.add(const DioLogger(redactedHeaders: {}));

⏱ Response Time

Elapsed time is shown automatically on the Time field of every RESPONSE and ERROR log.
Disable it with:

dio.interceptors.add(const DioLogger(logResponseTime: false));

🌀 cURL Export

Enable showCurl to print a ready-to-run curl command after each request. Headers respect your redactedHeaders config, and the body is formatted as -d for JSON or -F for FormData:

dio.interceptors.add(const DioLogger(showCurl: true));

Example output:

┌─ cURL ─────────────────────────────────
curl -X POST 'https://api.example.com/users' \
  -H 'content-type: application/json' \
  -H 'authorization: [REDACTED]' \
  -d '{"name":"Rasel"}'
└────────────────────────────────────────

♻️ Retry Logging

Enable logRetries to surface retry attempts in the log. In your retry interceptor, increment the counter in options.extra using the exported key constant:

import 'package:dio_ansi_logger/dio_ansi_logger.dart';

// Inside your retry interceptor's onRequest or onError:
options.extra[kDioAnsiLoggerRetryCount] =
    (options.extra[kDioAnsiLoggerRetryCount] ?? 0) + 1;

Then enable the flag on the logger:

dio.interceptors.add(const DioLogger(logRetries: true));

The logger will print ♻ Retry #1, ♻ Retry #2, etc. alongside the request log.


📝 AnsiLog — General Purpose Logger

AnsiLog can be called from anywhere — repositories, controllers, services, etc.

// Disable in release builds (call once in main.dart)
AnsiLog.enabled = kDebugMode;

AnsiLog.debug('User loaded: $user');
AnsiLog.info('Cache hit for key: $key');
AnsiLog.success('Payment completed');
AnsiLog.warning('Token expiring soon');
AnsiLog.error('Login failed', error: e);
AnsiLog.json(response.data, tag: 'GetTourApi');

📄 License

MIT © 2026 RASEL

Libraries

dio_ansi_logger
A beautiful, Postman-style Dio HTTP logger with ANSI colors and customizable themes for Dart & Flutter applications.