Commit d5fdfcb
feat(logging): stamp run/action context via LogRecordFactory (#1038)
## Summary
Replaces the per-logger `logging.Filter` approach for context stamping
with a global `LogRecordFactory`. The factory runs on every `LogRecord`
regardless of which logger created it, so any user-defined logger
automatically picks up `run_name`, `action_name`, and
`is_flyte_internal` attributes — no per-handler filter wiring required.
This also fixes a latent bug in main: the old `ContextFilter` and
`FlyteInternalFilter` mutated `record.msg` in place, so a record passing
through multiple handlers could get its `[run][action]` and `[flyte]`
prefixes prepended multiple times. Stamping attributes on the record
(and rendering them at format time) sidesteps that entirely.
## What changed
- **LogRecordFactory** (`src/flyte/_logging.py`) — stamps the active
flyte action context on every record, lazily importing `flyte._context`
to avoid import-time cycles.
- **`ContextFormatter`** replaces `ContextFilter` +
`FlyteInternalFilter`. The `[flyte]` internal-prefix marker is now a
formatter flag (`internal_prefix=True`), so the same record can be
formatted by multiple handlers without prefixes compounding.
- **`ContextFormatter(inner=...)`** — optional kwarg to delegate base
formatting to an existing `logging.Formatter`. Used to wrap pre-existing
root-handler formatters without losing their layout.
- **`initialize_logger(reset_root_logger=False)`** now wraps each
existing root-handler formatter with `ContextFormatter(inner=existing)`,
preserving main's "third-party log lines through root get
`[run][action]`" behavior. Idempotent on repeated init calls. Same
temporal limitation as main: handlers added after `flyte.init()` won't
be wrapped, but the factory still stamps the attrs so callers can format
them themselves.
- **`get_rich_handler(internal_prefix=True)`** — opt-out parameter; the
user-logger call site passes `False` so user-emitted lines don't carry
the `[flyte]` marker even under rich logging.
## Behavior parity vs main
| | main | this PR |
|---|---|---|
| `[run][action]` on flyte/flyte.user logs | ✅ | ✅ |
| `[run][action]` on third-party logs through root (when handler exists
at init) | ✅ (via filter) | ✅ (via formatter wrap) |
| `[run][action]` on third-party logs through root (handler added after
init) | ❌ | ❌ — but attrs still on record |
| `[flyte]` marker on flyte internal logs | ✅ | ✅ |
| Compounding prefixes when record passes through multiple handlers | ❌
(latent bug) | ✅ fixed |
| `record.run_name` / `action_name` available everywhere | partial | ✅
universal (factory) |
## Why this matters for users
`flyte.logger` and any child of `flyte.user` continue to work as before.
New: anyone using a stdlib logger (`logging.getLogger("myapp")`) outside
the flyte namespace also gets `run_name` / `action_name` /
`is_flyte_internal` stamped on every record by the factory — they just
need a formatter that references them (e.g.
`"[%(run_name)s][%(action_name)s] %(message)s"`).
## Example
`examples/basics/reuse_concurrent_logging.py` exercises three logger
configs with a reusable container running concurrent tasks, so log
output from multiple actions interleaves on a single stderr — each line
correctly prefixed by its own `[run][action]`:
1. `flyte.logger` — canonical user-facing logger.
2. `logging.getLogger("flyte.user.myapp")` — child of `flyte.user`,
inherits the formatter via propagation.
3. `logging.getLogger("myapp")` — fully independent logger with its own
handler that references the factory-stamped attrs in its format string.
## Test plan
- [x] `pytest tests/user_api/test_logging.py
tests/flyte/test_logging.py` passes (23 tests)
- [x] `test_user_logger_no_flyte_prefix` updated to check the formatter
flag instead of the removed `FlyteInternalFilter`
- [x] `test_user_logger_no_flyte_prefix_after_rich_init` — regression
test for rich-handler internal prefix bleeding into user logger
- [x] `test_initialize_logger_wraps_existing_root_handlers` — covers
wrap path + idempotency on repeated init
- [ ] Manual: run `examples/basics/reuse_concurrent_logging.py` against
a cluster and confirm all three worker variants emit
`[run][action]`-prefixed log lines on the shared reused container
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Haytham Abuelfutuh <haytham@afutuh.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>1 parent 31a7c93 commit d5fdfcb
3 files changed
Lines changed: 244 additions & 61 deletions
File tree
- examples/basics
- src/flyte
- tests/user_api
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
13 | 44 | | |
14 | 45 | | |
15 | 46 | | |
| |||
91 | 122 | | |
92 | 123 | | |
93 | 124 | | |
94 | | - | |
| 125 | + | |
95 | 126 | | |
96 | 127 | | |
97 | 128 | | |
| |||
117 | 148 | | |
118 | 149 | | |
119 | 150 | | |
120 | | - | |
| 151 | + | |
121 | 152 | | |
122 | 153 | | |
123 | 154 | | |
| |||
190 | 221 | | |
191 | 222 | | |
192 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
193 | 228 | | |
194 | 229 | | |
195 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
196 | 234 | | |
197 | 235 | | |
198 | 236 | | |
| |||
206 | 244 | | |
207 | 245 | | |
208 | 246 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 247 | + | |
215 | 248 | | |
216 | 249 | | |
217 | 250 | | |
| |||
231 | 264 | | |
232 | 265 | | |
233 | 266 | | |
234 | | - | |
| 267 | + | |
235 | 268 | | |
236 | 269 | | |
237 | 270 | | |
238 | | - | |
| 271 | + | |
239 | 272 | | |
240 | 273 | | |
241 | 274 | | |
242 | | - | |
| 275 | + | |
243 | 276 | | |
244 | | - | |
245 | 277 | | |
246 | 278 | | |
247 | 279 | | |
| |||
274 | 306 | | |
275 | 307 | | |
276 | 308 | | |
277 | | - | |
| 309 | + | |
278 | 310 | | |
279 | | - | |
280 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
281 | 315 | | |
282 | 316 | | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
298 | 328 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
313 | 341 | | |
314 | 342 | | |
315 | 343 | | |
| |||
330 | 358 | | |
331 | 359 | | |
332 | 360 | | |
| 361 | + | |
333 | 362 | | |
334 | | - | |
335 | | - | |
336 | 363 | | |
337 | | - | |
338 | 364 | | |
339 | 365 | | |
340 | 366 | | |
341 | 367 | | |
342 | 368 | | |
343 | 369 | | |
344 | 370 | | |
345 | | - | |
| 371 | + | |
346 | 372 | | |
347 | 373 | | |
348 | 374 | | |
349 | 375 | | |
350 | 376 | | |
351 | 377 | | |
352 | 378 | | |
353 | | - | |
354 | | - | |
| 379 | + | |
355 | 380 | | |
356 | 381 | | |
357 | 382 | | |
| |||
366 | 391 | | |
367 | 392 | | |
368 | 393 | | |
369 | | - | |
370 | 394 | | |
371 | 395 | | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
| 396 | + | |
377 | 397 | | |
378 | 398 | | |
379 | 399 | | |
| |||
0 commit comments