Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Hierarchical logging documentation #146

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
updates new readme entry for (hopefully) more clarity
  • Loading branch information
craiglabenz committed Jul 9, 2023
commit b1e3453ac874f543a68490fe734efa51608cddf2
67 changes: 40 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,55 @@ their `onRecord` streams.


```dart
hierarchicalLoggingEnabled = true;
hierarchicalLoggingEnabled = true;
Logger.root.level = Level.WARNING;
Logger.root.onRecord.listen((record) {
print('[ROOT][WARNING+] ${record.message}');
});

Logger.root.level = Level.FINE;
final log1 = Logger('FINE+');
log1.level = Level.FINE;
log1.onRecord.listen((record) {
print('[LOG1][FINE+] ${record.message}');
});

final log1 = Logger('WARNING+');
log1.level = Level.WARNING;
Logger.root.onRecord.listen((record) {
print('[WARNING+] ${record.message}');
});
// log2 inherits LEVEL value of WARNING from `Logger.root`
final log2 = Logger('WARNING+');
log2.onRecord.listen((record) {
print('[LOG2][WARNING+] ${record.message}');
});

final log2 = Logger('FINE+'); // Inherited from `Logger.root`
log2.onRecord.listen((record) {
print('[FINE+] ${record.message}');
});

log1.info('Will not print because too low level');
log2.info(
'WILL print TWICE ([FINE+] and [WARNING+]) '
'because `log2` uses individual and root listeners',
);

log1.warning('WILL print ONCE because `log1` only uses root listener');
log2.warning(
'WILL print TWICE because `log2` '
'uses individual and root listeners',
);
// Will NOT print because FINER is too low level for `Logger.root`.
log1.finer('LOG_01 FINER (X)');

// Will print twice ([LOG1] & [ROOT])
log1.fine('LOG_01 FINE (√√)');

// Will print ONCE because `log1` only uses root listener.
log1.warning('LOG_01 WARNING (√)');

// Will never print because FINE is too low level.
log2.fine('LOG_02 FINE (X)');

// Will print twice ([LOG2] & [ROOT]) because warning is sufficient for all
// loggers' levels.
log2.warning('LOG_02 WARNING (√√)');

// Will never print because `info` is filtered by `Logger.root.level` of
// `Level.WARNING`.
log2.info('INFO (X)');
```

Results in:

```
[FINE+] WILL print TWICE ([FINE+] and [WARNING+]) because `log2` uses individual and root listeners
[WARNING+] WILL print TWICE ([FINE+] and [WARNING+]) because `log2` uses individual and root listeners
[WARNING+] WILL print ONCE because `log1` only uses root listener
[FINE+] WILL print TWICE because `log2` uses individual and root listeners
[WARNING+] WILL print TWICE because `log2` uses individual and root listeners
[LOG1][FINE+] LOG_01 FINE (√√)
[ROOT][WARNING+] LOG_01 FINE (√√)
[LOG1][FINE+] LOG_01 WARNING (√)
[ROOT][WARNING+] LOG_01 WARNING (√)
[LOG2][WARNING+] LOG_02 WARNING (√√)
[ROOT][WARNING+] LOG_02 WARNING (√√)
```

## Publishing automation
Expand Down