Files
cherrypick/website/docs/advanced-features/logging.md
Sergey Penkovsky 248bf4c8c5 feat(website): update home page to showcase CherryPick DI documentation
- Replaced the main action button text with 'Explore CherryPick Documentation 🍒' instead of 'Docusaurus Tutorial'.
- Updated the button link to target /docs/intro (main docs entry point).
- Changed <Layout> props:
  - Page title now uses project title only (siteConfig.title)
  - Added a CherryPick-related site description for better SEO and context.
- The homepage is now tailored to reflect CherryPick's purpose as a Dart & Flutter DI library instead of Docusaurus boilerplate.
2025-08-14 13:41:54 +03:00

2.1 KiB

sidebar_position
sidebar_position
2

Logging

CherryPick lets you log all dependency injection (DI) events and errors using a flexible observer mechanism.

Custom Observers

You can pass any implementation of CherryPickObserver to your root scope or any sub-scope. This allows centralized and extensible logging, which you can direct to print, files, visualization frameworks, external loggers, or systems like Talker.

Example: Printing All Events

import 'package:cherrypick/cherrypick.dart';

void main() {
  // Use the built-in PrintCherryPickObserver for console logs
  final observer = PrintCherryPickObserver();
  final scope = CherryPick.openRootScope(observer: observer);
  // All DI actions and errors will now be printed!
}

Example: Advanced Logging with Talker

For richer logging, analytics, or UI overlays, use an advanced observer such as talker_cherrypick_logger:

import 'package:cherrypick/cherrypick.dart';
import 'package:talker/talker.dart';
import 'package:talker_cherrypick_logger/talker_cherrypick_logger.dart';

void main() {
  final talker = Talker();
  final observer = TalkerCherryPickObserver(talker);
  CherryPick.openRootScope(observer: observer);
  // All container events go to the Talker log system!
}

Default Behavior

  • By default, logging is silent (SilentCherryPickObserver) for production, with no output unless you supply an observer.
  • You can configure observers per scope for isolated, test-specific, or feature-specific logging.

Observer Capabilities

Events you can observe and log:

  • Dependency registration
  • Instance requests, creations, disposals
  • Module installs/removals
  • Scope opening/closing
  • Cache hits/misses
  • Cycle detection
  • Diagnostics, warnings, errors

Just implement or extend CherryPickObserver and direct messages anywhere you want!

When to Use

  • Enable verbose logging and debugging in development or test builds.
  • Route logs to your main log system or analytics.
  • Hook into DI lifecycle for profiling or monitoring.