mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
docs(logging): update Logging section in README with modern Observer usage and Talker integration examples
This commit is contained in:
@@ -422,30 +422,68 @@ final dialogManager = dialogScope.resolve<DialogManager>();
|
|||||||
|
|
||||||
### Logging
|
### Logging
|
||||||
|
|
||||||
CherryPick supports centralized logging of all dependency injection (DI) events and errors. You can globally enable logs for your application or test environment with:
|
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](https://pub.dev/packages/talker).
|
||||||
|
|
||||||
|
##### Example: Printing All Events
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// Set a global logger before any scopes are created
|
// Use the built-in PrintCherryPickObserver for console logs
|
||||||
CherryPick.setGlobalLogger(PrintLogger()); // or your custom logger
|
final observer = PrintCherryPickObserver();
|
||||||
|
final scope = CherryPick.openRootScope(observer: observer);
|
||||||
final scope = CherryPick.openRootScope();
|
// All DI actions and errors will now be printed!
|
||||||
// All DI actions and errors will now be logged!
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- All dependency resolution, scope creation, module installation, and circular dependency errors will be sent to your logger (via info/error method).
|
|
||||||
- By default, logs are off (SilentLogger is used in production).
|
|
||||||
|
|
||||||
If you want fine-grained, test-local, or isolated logging, you can provide a logger directly to each scope:
|
##### Example: Advanced Logging with Talker
|
||||||
|
|
||||||
|
For richer logging, analytics, or UI overlays, use an advanced observer such as [talker_cherrypick_logger](../talker_cherrypick_logger):
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
final logger = MockLogger();
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
final scope = Scope(null, logger: logger); // works in tests for isolation
|
import 'package:talker/talker.dart';
|
||||||
scope.installModules([...]);
|
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.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Circular Dependency Detection
|
### Circular Dependency Detection
|
||||||
|
|||||||
Reference in New Issue
Block a user