Skip to content

Commit b539ee9

Browse files
authored
Hierarchical logging documentation (dart-archive/logging#146)
1 parent bef87f3 commit b539ee9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkgs/logging/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,69 @@ Available logging methods are:
7777
+ `log.finer(logged_content);`
7878
+ `log.finest(logged_content);`
7979

80+
## Configuration
81+
82+
Loggers can be individually configured and listened to. When an individual logger has no
83+
specific configuration, it uses the configuration and any listeners found at `Logger.root`.
84+
85+
To begin, set the global boolean `hierarchicalLoggingEnabled` to `true`.
86+
87+
Then, create unique loggers and configure their `level` attributes and assign any listeners to
88+
their `onRecord` streams.
89+
90+
91+
```dart
92+
hierarchicalLoggingEnabled = true;
93+
Logger.root.level = Level.WARNING;
94+
Logger.root.onRecord.listen((record) {
95+
print('[ROOT][WARNING+] ${record.message}');
96+
});
97+
98+
final log1 = Logger('FINE+');
99+
log1.level = Level.FINE;
100+
log1.onRecord.listen((record) {
101+
print('[LOG1][FINE+] ${record.message}');
102+
});
103+
104+
// log2 inherits LEVEL value of WARNING from `Logger.root`
105+
final log2 = Logger('WARNING+');
106+
log2.onRecord.listen((record) {
107+
print('[LOG2][WARNING+] ${record.message}');
108+
});
109+
110+
111+
// Will NOT print because FINER is too low level for `Logger.root`.
112+
log1.finer('LOG_01 FINER (X)');
113+
114+
// Will print twice ([LOG1] & [ROOT])
115+
log1.fine('LOG_01 FINE (√√)');
116+
117+
// Will print ONCE because `log1` only uses root listener.
118+
log1.warning('LOG_01 WARNING (√)');
119+
120+
// Will never print because FINE is too low level.
121+
log2.fine('LOG_02 FINE (X)');
122+
123+
// Will print twice ([LOG2] & [ROOT]) because warning is sufficient for all
124+
// loggers' levels.
125+
log2.warning('LOG_02 WARNING (√√)');
126+
127+
// Will never print because `info` is filtered by `Logger.root.level` of
128+
// `Level.WARNING`.
129+
log2.info('INFO (X)');
130+
```
131+
132+
Results in:
133+
134+
```
135+
[LOG1][FINE+] LOG_01 FINE (√√)
136+
[ROOT][WARNING+] LOG_01 FINE (√√)
137+
[LOG1][FINE+] LOG_01 WARNING (√)
138+
[ROOT][WARNING+] LOG_01 WARNING (√)
139+
[LOG2][WARNING+] LOG_02 WARNING (√√)
140+
[ROOT][WARNING+] LOG_02 WARNING (√√)
141+
```
142+
80143
## Publishing automation
81144

82145
For information about our publishing automation and release process, see

0 commit comments

Comments
 (0)