Skip to content

feat(api): add lightweight Dio request/response logging interceptor#9

Merged
CowboyGH merged 2 commits intodevelopfrom
feature/api-interceptor-logging
Feb 19, 2026
Merged

feat(api): add lightweight Dio request/response logging interceptor#9
CowboyGH merged 2 commits intodevelopfrom
feature/api-interceptor-logging

Conversation

@CowboyGH
Copy link
Copy Markdown
Owner

@CowboyGH CowboyGH commented Feb 19, 2026

🚀 Summary

Added minimal Dio logging interceptor for development to make HTTP debugging easier and cleaner.

✨ Changes

  • ✅ Added debug-only network interceptor that logs request/response/error metadata (no bodies) to improve development troubleshooting.

🧪 Verification

  • flutter analyze
  • Manual debug run: request/response/error logs are printed in compact format

@CowboyGH CowboyGH self-assigned this Feb 19, 2026
@CowboyGH CowboyGH added area: network API, requests, and data parsing type: feature New feature or request labels Feb 19, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 19, 2026

📝 Walkthrough

Walkthrough

A debug-only Dio InterceptorWrapper was added in the DI setup to log request/response/error metadata (excluding bodies) when running in debug mode; a CHANGELOG entry documenting this addition was also added.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added an Unreleased note describing the new debug-only Dio InterceptorsWrapper that logs request/response/error metadata without bodies.
DI & Logging Setup
lib/core/di/di.dart
Imported Flutter foundation and registered a debug-only Dio interceptor inside setupDI that logs request, response, and error metadata via AppLogger when kDebugMode is true; no non-debug behavior or public API changes.

Sequence Diagram(s)

sequenceDiagram
    participant App as App
    participant DI as DI setup
    participant Dio as Dio Client
    participant Interceptor as Debug Interceptor
    participant Logger as AppLogger

    App->>DI: initialize setupDI()
    DI->>Dio: register/debug-add Interceptor
    App->>Dio: make HTTP request
    Dio->>Interceptor: onRequest(metadata)
    Interceptor->>Logger: log request metadata
    Dio->>Interceptor: onResponse(metadata)
    Interceptor->>Logger: log response metadata
    Dio->>Interceptor: onError(metadata)
    Interceptor->>Logger: log error metadata
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Possibly related PRs

Poem

🐰 I hopped into DI, ears all keen,

Logging the metadata, quiet and clean,
No bodies displayed, just traces to see,
In debug I whisper, "Safe logs from me." 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately describes the main change: adding a lightweight Dio request/response logging interceptor. It aligns with the changeset which introduces debug-only request/response/error logging via an InterceptorsWrapper.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/api-interceptor-logging

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
lib/core/di/di.dart (2)

66-71: Consider logging error.type for network-level failures

When there's no HTTP response (timeouts, connection refused, cancellation), the log currently emits x - METHOD URI with no indication of why it failed. Adding error.type makes these cases immediately recognisable.

♻️ Proposed fix
        onError: (DioException error, handler) {
          final request = error.requestOptions;
-         di<AppLogger>().w(
-           'x ${error.response?.statusCode ?? '-'} ${request.method} ${request.uri} ${error.message ?? ''}',
-         );
+         logger.w(
+           'x ${error.response?.statusCode ?? '-'} [${error.type.name}] ${request.method} ${request.uri} ${error.message ?? ''}',
+         );
          return handler.next(error);
        },
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/core/di/di.dart` around lines 66 - 71, The onError handler for
DioException should include network-level failure info by logging error.type in
addition to the existing fields; update the onError block (the DioException
error handler that constructs the log in di<AppLogger>().w using request,
error.response?.statusCode and error.message) to append or interpolate
error.type so timeouts, cancellations and connection failures are identifiable
in the log message.

53-75: Resolve AppLogger once outside the interceptor callbacks

di<AppLogger>() is called on every request, response, and error. While it is a cheap singleton lookup, capturing it once before the InterceptorsWrapper is cleaner and more idiomatic.

♻️ Proposed refactor
  if (kDebugMode) {
+   final logger = di<AppLogger>();
    dio.interceptors.add(
      InterceptorsWrapper(
        onRequest: (options, handler) {
-         di<AppLogger>().d('-> ${options.method} ${options.uri}');
+         logger.d('-> ${options.method} ${options.uri}');
          return handler.next(options);
        },
        onResponse: (response, handler) {
-         di<AppLogger>().d(
+         logger.d(
            '<- ${response.statusCode} ${response.requestOptions.method} ${response.requestOptions.uri}',
          );
          return handler.next(response);
        },
        onError: (DioException error, handler) {
          final request = error.requestOptions;
-         di<AppLogger>().w(
+         logger.w(
            'x ${error.response?.statusCode ?? '-'} ${request.method} ${request.uri} ${error.message ?? ''}',
          );
          return handler.next(error);
        },
      ),
    );
  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/core/di/di.dart` around lines 53 - 75, The interceptor repeatedly
resolves di<AppLogger>() inside onRequest, onResponse, and onError; capture the
AppLogger once before creating the InterceptorsWrapper (e.g., final logger =
di<AppLogger>()), then use logger in the onRequest, onResponse, and onError
callbacks when adding the InterceptorsWrapper to dio.interceptors via
dio.interceptors.add; update references to di<AppLogger>() in those callbacks to
use the local logger variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lib/core/di/di.dart`:
- Around line 66-71: The onError handler for DioException should include
network-level failure info by logging error.type in addition to the existing
fields; update the onError block (the DioException error handler that constructs
the log in di<AppLogger>().w using request, error.response?.statusCode and
error.message) to append or interpolate error.type so timeouts, cancellations
and connection failures are identifiable in the log message.
- Around line 53-75: The interceptor repeatedly resolves di<AppLogger>() inside
onRequest, onResponse, and onError; capture the AppLogger once before creating
the InterceptorsWrapper (e.g., final logger = di<AppLogger>()), then use logger
in the onRequest, onResponse, and onError callbacks when adding the
InterceptorsWrapper to dio.interceptors via dio.interceptors.add; update
references to di<AppLogger>() in those callbacks to use the local logger
variable.

…AppLogger once outside the interceptor callbacks
@CowboyGH CowboyGH merged commit 7a06f76 into develop Feb 19, 2026
3 of 4 checks passed
@CowboyGH CowboyGH deleted the feature/api-interceptor-logging branch February 19, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: network API, requests, and data parsing type: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant