Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@techouse
Copy link
Contributor

@techouse techouse commented May 7, 2025

This change fixes the issue #365 where passing a custom TalkerLogger into TalkerFlutter.init would still have its output callback unconditionally overridden by the default Flutter output function. Now, if you supply your own TalkerLogger, its output delegate is used verbatim; the default only applies when you don’t pass any logger at all.

Before

// Always overwrote custom logger.output:
Talker(
  logger: (logger ?? TalkerLogger()).copyWith(
    output: _defaultFlutterOutput,
  ),
  …
);
  • Even if you passed in a TalkerLogger(output: myOutput), .copyWith(output: _defaultFlutterOutput) forced it back to the default.

After

// Only uses default output when no logger is provided:
Talker(
  logger: logger ?? TalkerLogger(output: _defaultFlutterOutput),
  …
);
  • Custom logger: Talker.init(logger: customLogger) -> uses customLogger.output unchanged.
  • No logger: defaults to TalkerLogger(output: _defaultFlutterOutput) for platform-aware printing.

Benefits:

  • Allows full customization of logging behavior without being overridden.
  • Maintains existing default behavior for callers who don’t provide a logger.
  • Simplifies the initialization logic by removing an unnecessary copyWith.

tl;dr fixes #365

Summary by Sourcery

Fix the handling of custom TalkerLogger output in TalkerFlutter initialization to respect user-provided logging configurations

New Features:

  • Add support for preserving custom logger output configurations

Bug Fixes:

  • Prevent unconditional overriding of custom logger output in TalkerFlutter.init
  • Ensure custom logger output is used when provided

Enhancements:

  • Improve platform-specific logging output handling
  • Simplify logger initialization logic

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 7, 2025

Reviewer's Guide

The TalkerFlutter.init method was updated to respect a custom TalkerLogger's output delegate if one is provided, instead of unconditionally overriding it. This was achieved by changing the logger instantiation from (logger ?? TalkerLogger()).copyWith(output: _defaultFlutterOutput) to logger ?? TalkerLogger(output: _defaultFlutterOutput), ensuring the default Flutter-specific output is only used if no logger is explicitly passed. The _defaultFlutterOutput method was also refactored for clarity, using a switch statement for platform detection and a specific import for dart:developer.log.

Sequence Diagram: Logger Initialization with Custom Logger (Before PR)

sequenceDiagram
    actor Developer
    participant TF_init as TalkerFlutter.init
    participant customLogger as Custom TalkerLogger
    participant CL_copyWith as customLogger.copyWith
    participant Talker_ctor as Talker Constructor

    Developer->>+TF_init: init(logger: customLogger)
    Note over TF_init: customLogger has its own output function
    TF_init->>customLogger: Access customLogger
    TF_init->>+CL_copyWith: customLogger.copyWith(output: _defaultFlutterOutput)
    Note right of CL_copyWith: Overrides custom output with _defaultFlutterOutput
    CL_copyWith-->>-TF_init: loggerWithOverriddenOutput
    TF_init->>+Talker_ctor: Talker(logger: loggerWithOverriddenOutput, ...)
    Talker_ctor-->>-TF_init: talkerInstance
    TF_init-->>-Developer: talkerInstance
    Note over Developer: Custom logger's output was NOT used.
Loading

Sequence Diagram: Logger Initialization with Custom Logger (After PR)

sequenceDiagram
    actor Developer
    participant TF_init as TalkerFlutter.init
    participant customLogger as Custom TalkerLogger
    participant Talker_ctor as Talker Constructor

    Developer->>+TF_init: init(logger: customLogger)
    Note over TF_init: customLogger has its own output function
    TF_init->>+Talker_ctor: Talker(logger: customLogger, ...)
    Note right of Talker_ctor: customLogger (with its original output) is used directly
    Talker_ctor-->>-TF_init: talkerInstance
    TF_init-->>-Developer: talkerInstance
    Note over Developer: Custom logger's output IS respected.
Loading

Sequence Diagram: Logger Initialization with Default Logger (After PR)

sequenceDiagram
    actor Developer
    participant TF_init as TalkerFlutter.init
    participant New_TL as new TalkerLogger
    participant Talker_ctor as Talker Constructor

    Developer->>+TF_init: init(logger: null) / init()
    Note over TF_init: No custom logger provided
    TF_init->>+New_TL: TalkerLogger(output: _defaultFlutterOutput)
    New_TL-->>-TF_init: defaultLogger
    TF_init->>+Talker_ctor: Talker(logger: defaultLogger, ...)
    Talker_ctor-->>-TF_init: talkerInstance
    TF_init-->>-Developer: talkerInstance
    Note over Developer: Default Flutter-specific logger is used.
Loading

File-Level Changes

Change Details Files
Modified TalkerFlutter.init to preserve custom logger output and only apply default output when no logger is provided.
  • Changed logger instantiation to logger ?? TalkerLogger(output: _defaultFlutterOutput).
  • Removed unconditional override of custom logger's output with copyWith.
packages/talker_flutter/lib/src/extensions/talker_flutter.dart
Refactored the _defaultFlutterOutput method for improved clarity and import specificity.
  • Replaced an if-else if structure with a switch statement for platform-specific logging.
  • Changed import 'dart:developer'; to import 'dart:developer' show log;.
  • Changed method return type from dynamic to void.
packages/talker_flutter/lib/src/extensions/talker_flutter.dart
Updated dependency overrides.
  • Added talker_logger to dependency_overrides.
packages/talker_flutter/pubspec_overrides.yaml

Assessment against linked issues

Issue Objective Addressed Explanation
#365 Allow users to modify log messages before they are printed. βœ…
#365 Provide a way to sanitize all logs. βœ…
#365 Ensure that custom output functions provided by the user are respected and not overridden by default settings. βœ…

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

@techouse techouse changed the title πŸ› Respect custom TalkerLogger output in TalkerFlutter.init πŸ› Respect custom TalkerLogger output in TalkerFlutter.init May 7, 2025
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 @techouse - I've reviewed your changes - here's some feedback:

  • The changes to pubspec_overrides.yaml appear to be local development overrides; please confirm if they are intended for this PR.
Here's what I looked at during the review
  • 🟒 General issues: all looks good
  • 🟒 Security: all looks good
  • 🟒 Testing: all looks good
  • 🟒 Complexity: all looks good
  • 🟒 Documentation: all looks good

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.

@techouse techouse changed the title πŸ› Respect custom TalkerLogger output in TalkerFlutter.init fix: Respect custom TalkerLogger output in TalkerFlutter.init May 7, 2025
@techouse techouse changed the title fix: Respect custom TalkerLogger output in TalkerFlutter.init fix: respect custom TalkerLogger output in TalkerFlutter.init May 7, 2025
@Frezyx Frezyx merged commit 6c4b1ba into Frezyx:master May 19, 2025
1 check passed
@techouse techouse deleted the fix/issue-365-intercept-talker-flutter-log branch May 19, 2025 11:25
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.

How to intercept the logs?

2 participants