Skip to content

Comments

Add custom pretty formatter and strip quotes option #2#465

Open
westito wants to merge 3 commits intoFrezyx:masterfrom
westito:pretty-formatter-update-2
Open

Add custom pretty formatter and strip quotes option #2#465
westito wants to merge 3 commits intoFrezyx:masterfrom
westito:pretty-formatter-update-2

Conversation

@westito
Copy link
Contributor

@westito westito commented Dec 31, 2025

Summary

  • Add prettyFormatter setting to customize data formatting (that overrides default JsonEncoder)
  • Add stripJsonQuotes setting to strip double quotes from JSON output while preserving escaped quotes in values

Example

TalkerDioLogger(
  settings: TalkerDioLoggerSettings(
    stripJsonQuotes: true,
  ),
)
Before:
{
  "name": "John \"Doe\"",
  "age": 30
}

After:
{
  name: John "Doe",
  age: 30
}

Summary by Sourcery

Introduce a configurable JSON formatting utility and integrate it into HTTP/Dio/Chopper loggers to support custom pretty-printing and optional quote stripping.

New Features:

  • Add TalkerJsonFormatter utility with support for default pretty JSON, quote-stripping mode, and fully custom formatter functions.
  • Expose jsonFormatter settings on Dio, HTTP, and Chopper logger settings to allow customization of how request/response data, headers, and extras are formatted in logs.

Enhancements:

  • Refactor Dio, HTTP, and Chopper loggers to use the shared TalkerJsonFormatter instead of hardcoded JsonEncoder-based formatting, while preserving existing behavior by default.

Tests:

  • Add comprehensive unit tests for TalkerJsonFormatter behavior, including quote-stripping, custom formatters, and edge cases.
  • Extend Dio logger tests to cover jsonFormatter usage, quote stripping on data/headers/extra, and precedence of responseDataConverter over jsonFormatter.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 31, 2025

Reviewer's Guide

Introduces a reusable TalkerJsonFormatter utility that supports default pretty JSON, quote-stripping, and custom formatting callbacks, and wires it into the Dio, HTTP, and Chopper loggers via new jsonFormatter settings while updating logs/tests to exercise the new behaviors and precedence with existing responseDataConverter logic.

Sequence diagram for Dio response logging with custom formatter and stripQuotes

sequenceDiagram
  participant Caller
  participant DioResponseLog
  participant TalkerDioLoggerSettings
  participant TalkerJsonFormatter

  Caller->>DioResponseLog: generateTextMessage()
  DioResponseLog->>TalkerDioLoggerSettings: access responseDataConverter
  alt responseDataConverter is not null
    DioResponseLog->>TalkerDioLoggerSettings: responseDataConverter(response)
    DioResponseLog-->>Caller: message with converted data
  else responseDataConverter is null
    DioResponseLog->>TalkerDioLoggerSettings: access jsonFormatter
    DioResponseLog->>TalkerJsonFormatter: format(data)
    TalkerJsonFormatter-->>DioResponseLog: formattedJson
    DioResponseLog-->>Caller: message with formattedJson
  end
Loading

Class diagram for TalkerJsonFormatter integration into logger settings

classDiagram
  class TalkerJsonFormatter {
    +bool stripQuotes
    -String Function(dynamic data) _customFormatter
    +TalkerJsonFormatter(stripQuotes bool stripQuotes)
    +TalkerJsonFormatter.custom(String Function(dynamic data) customFormatter)
    +String format(dynamic data)
  }

  class TalkerDioLoggerSettings {
    +TalkerDioLoggerSettings(...)
    +TalkerDioLoggerSettings copyWith(..., TalkerJsonFormatter jsonFormatter, ...)
    +TalkerJsonFormatter jsonFormatter
    +String Function(Response response) responseDataConverter
  }

  class TalkerHttpLoggerSettings {
    +TalkerHttpLoggerSettings(...)
    +TalkerHttpLoggerSettings copyWith(..., TalkerJsonFormatter jsonFormatter, ...)
    +TalkerJsonFormatter jsonFormatter
  }

  class TalkerChopperLoggerSettings {
    +TalkerChopperLoggerSettings(...)
    +TalkerChopperLoggerSettings copyWith(..., TalkerJsonFormatter jsonFormatter, ...)
    +TalkerJsonFormatter jsonFormatter
  }

  class DioRequestLog {
    +RequestOptions requestOptions
    +TalkerDioLoggerSettings settings
    +String generateTextMessage(...)
    +String Function(dynamic data) _format
  }

  class DioResponseLog {
    +Response response
    +TalkerDioLoggerSettings settings
    +int responseTime
    +String generateTextMessage(...)
    +String Function(dynamic data) _format
  }

  class DioErrorLog {
    +DioException error
    +TalkerDioLoggerSettings settings
    +int responseTime
    +String generateTextMessage(...)
    +String Function(dynamic data) _format
  }

  class HttpRequestLog {
    +BaseRequest request
    +TalkerHttpLoggerSettings settings
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  class HttpResponseLog {
    +BaseResponse response
    +TalkerHttpLoggerSettings settings
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  class HttpErrorLog {
    +BaseResponse response
    +TalkerHttpLoggerSettings settings
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  class ChopperRequestLog {
    +Request request
    +TalkerChopperLoggerSettings settings
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  class ChopperResponseLog {
    +Response response
    +TalkerChopperLoggerSettings settings
    +int responseTime
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  class ChopperErrorLog {
    +Request request
    +TalkerChopperLoggerSettings settings
    +int responseTime
    +String generateTextMessage(...)
    +String convert(Object object)
  }

  TalkerDioLoggerSettings --> TalkerJsonFormatter : uses
  TalkerHttpLoggerSettings --> TalkerJsonFormatter : uses
  TalkerChopperLoggerSettings --> TalkerJsonFormatter : uses

  DioRequestLog --> TalkerDioLoggerSettings : has
  DioResponseLog --> TalkerDioLoggerSettings : has
  DioErrorLog --> TalkerDioLoggerSettings : has

  HttpRequestLog --> TalkerHttpLoggerSettings : has
  HttpResponseLog --> TalkerHttpLoggerSettings : has
  HttpErrorLog --> TalkerHttpLoggerSettings : has

  ChopperRequestLog --> TalkerChopperLoggerSettings : has
  ChopperResponseLog --> TalkerChopperLoggerSettings : has
  ChopperErrorLog --> TalkerChopperLoggerSettings : has

  DioRequestLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  DioResponseLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  DioErrorLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter

  HttpRequestLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  HttpResponseLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  HttpErrorLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter

  ChopperRequestLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  ChopperResponseLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
  ChopperErrorLog ..> TalkerJsonFormatter : calls format via settings.jsonFormatter
Loading

File-Level Changes

Change Details Files
Replace hardcoded JsonEncoder usage in Dio logs with a pluggable formatter sourced from TalkerDioLoggerSettings.jsonFormatter.
  • Remove the local JsonEncoder usage and hidden _encoder constant from DioRequestLog, DioResponseLog, and DioErrorLog.
  • Add a private _format getter that delegates to settings.jsonFormatter.format.
  • Use _format for request/response/extra data, and headers formatting instead of JsonEncoder.withIndent.
  • Ensure responseDataConverter still has precedence over jsonFormatter for response data.
packages/talker_dio_logger/lib/dio_logs.dart
Expose jsonFormatter configuration on logger settings types so callers can control JSON formatting (default, stripQuotes, or custom).
  • Add a TalkerJsonFormatter jsonFormatter field with default const TalkerJsonFormatter() to TalkerDioLoggerSettings, TalkerHttpLoggerSettings, and TalkerChopperLoggerSettings.
  • Extend copyWith methods to accept and propagate jsonFormatter.
  • Update Equatable props to include jsonFormatter where applicable.
packages/talker_dio_logger/lib/talker_dio_logger_settings.dart
packages/talker_http_logger/lib/talker_http_logger_settings.dart
packages/talker_chopper_logger/lib/talker_chopper_logger_settings.dart
Refactor Chopper and HTTP loggers to use the shared TalkerJsonFormatter instead of local JsonEncoder instances.
  • Remove JsonEncoder imports and static _encoder fields from ChopperLog and HttpLog classes.
  • Change the convert helper in these log classes to call settings.jsonFormatter.format.
  • Keep existing JSON parsing logic (jsonDecode) where present while delegating stringification to the formatter.
packages/talker_chopper_logger/lib/chopper_error_log.dart
packages/talker_chopper_logger/lib/chopper_response_log.dart
packages/talker_chopper_logger/lib/chopper_request_log.dart
packages/talker_http_logger/lib/http_error_log.dart
packages/talker_http_logger/lib/http_response_log.dart
packages/talker_http_logger/lib/http_request_log.dart
Introduce TalkerJsonFormatter utility with support for quote-stripping and custom formatting, and export it from the main Talker utils API.
  • Add a new json_formatter.dart defining TalkerJsonFormatter with default pretty JSON encoding via JsonEncoder.withIndent.
  • Implement stripQuotes behavior that removes double quotes while preserving escaped quotes using a placeholder strategy.
  • Add a .custom constructor that accepts a String Function(dynamic) and disables stripQuotes.
  • Export json_formatter.dart from utils.dart so it is available via package:talker/talker.dart.
packages/talker/lib/src/utils/json_formatter.dart
packages/talker/lib/src/utils/utils.dart
Add comprehensive tests for TalkerJsonFormatter and new json formatting behavior across Dio logs (request, response, error).
  • Create json_formatter_test.dart validating default formatting, stripQuotes behavior (including nested, arrays, and edge cases), custom formatter semantics, and edge conditions like null/empty structures.
  • Extend logs_test.dart to cover stripQuotes for request/response/error data and headers, custom jsonFormatter usage, and precedence of responseDataConverter over jsonFormatter.
  • Assert that when stripQuotes is true, keys and string values lose surrounding quotes but escaped quotes inside values are preserved.
packages/talker/test/json_formatter_test.dart
packages/talker_dio_logger/test/logs_test.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov-commenter
Copy link

codecov-commenter commented Dec 31, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.09%. Comparing base (1ed15ab) to head (26a45ab).
⚠️ Report is 172 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #465      +/-   ##
==========================================
- Coverage   98.63%   97.09%   -1.54%     
==========================================
  Files           3       30      +27     
  Lines         146      896     +750     
==========================================
+ Hits          144      870     +726     
- Misses          2       26      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants