Commit 4722f5e
committed
Use Python's logging framework for INFO messages in core.py
Previously, the two 'INFO: Showing help …' diagnostics were emitted via
bare print() calls to sys.stderr. This made it impossible for users to
suppress or redirect the messages using the standard logging machinery.
This commit replaces those calls with a proper Python logging setup:
* Adds _LazyStderrStreamHandler – a StreamHandler subclass that resolves
sys.stderr at emit time (via a property) rather than at construction time.
This is required so that unit-test code that patches sys.stderr with an
in-memory buffer continues to work correctly.
* Adds _logger = logging.getLogger('fire.core') and installs the lazy
handler on it with level NOTSET, so the effective level is inherited from
the parent 'fire' logger.
* Sets the parent 'fire' logger to INFO (only if it has not already been
configured) so that the default user experience is identical to before:
the INFO line still appears on stderr without any logging configuration.
* Users can now suppress the message with a single line:
import logging; logging.getLogger('fire').setLevel(logging.WARNING)
or redirect it to an arbitrary destination by adding their own handler to
logging.getLogger('fire') or logging.getLogger('fire.core').
Adds four new tests in LoggingTest covering:
- Default stderr output is preserved.
- _logger is a logging.Logger named 'fire.core'.
- INFO can be suppressed via the fire.core logger level.
- INFO can be suppressed via the parent fire logger level.
- INFO can be redirected through a custom handler.
All 265 existing tests continue to pass.
Fixes: #3531 parent 716bbc2 commit 4722f5e
2 files changed
+116
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
73 | 119 | | |
74 | 120 | | |
75 | 121 | | |
| |||
231 | 277 | | |
232 | 278 | | |
233 | 279 | | |
234 | | - | |
235 | | - | |
| 280 | + | |
236 | 281 | | |
237 | 282 | | |
238 | 283 | | |
| |||
287 | 332 | | |
288 | 333 | | |
289 | 334 | | |
290 | | - | |
291 | | - | |
| 335 | + | |
292 | 336 | | |
293 | 337 | | |
294 | 338 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
224 | 227 | | |
225 | 228 | | |
226 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
227 | 295 | | |
228 | 296 | | |
0 commit comments