Add on_missing_context parameter to get_run_logger()#21028
Add on_missing_context parameter to get_run_logger()#21028zionts wants to merge 6 commits intoPrefectHQ:mainfrom
Conversation
get_run_logger() raises MissingContextError when called outside a flow or task run context. This makes it unusable from threads (e.g. ThreadPoolExecutor workers) where no Prefect context exists, forcing users to monkey-patch the function or wrap every call in try/except. Add an on_missing_context parameter that controls this behavior: - "raise" (default): current behavior, fully backward compatible - "warn": return a standard prefect logger + emit debug message - "ignore": return a standard prefect logger silently This lets users write code that works both inside and outside Prefect run contexts without monkey-patching.
| Get a Prefect logger for the current task run or flow run. | ||
|
|
||
| The logger will be named either `prefect.task_runs` or `prefect.flow_runs`. | ||
| The logger will be named either ``prefect.task_runs`` or ``prefect.flow_runs``. |
There was a problem hiding this comment.
| The logger will be named either ``prefect.task_runs`` or ``prefect.flow_runs``. | |
| The logger will be named either `prefect.task_runs` or `prefect.flow_runs`. |
| Contextual data about the run will be attached to the log records. | ||
|
|
||
| These loggers are connected to the `APILogHandler` by default to send log records to | ||
| These loggers are connected to the ``APILogHandler`` by default to send log records to |
There was a problem hiding this comment.
| These loggers are connected to the ``APILogHandler`` by default to send log records to | |
| These loggers are connected to the `APILogHandler` by default to send log records to |
| on_missing_context: Controls behavior when no active flow or task run context | ||
| exists. ``"raise"`` (default) raises ``MissingContextError`` for backward | ||
| compatibility. ``"warn"`` returns a standard ``prefect`` logger and emits a | ||
| warning. ``"ignore"`` returns a standard ``prefect`` logger silently. Use | ||
| ``"warn"`` or ``"ignore"`` when calling from threads or other contexts where | ||
| a Prefect run context may not be available. |
There was a problem hiding this comment.
| on_missing_context: Controls behavior when no active flow or task run context | |
| exists. ``"raise"`` (default) raises ``MissingContextError`` for backward | |
| compatibility. ``"warn"`` returns a standard ``prefect`` logger and emits a | |
| warning. ``"ignore"`` returns a standard ``prefect`` logger silently. Use | |
| ``"warn"`` or ``"ignore"`` when calling from threads or other contexts where | |
| a Prefect run context may not be available. | |
| on_missing_context: Controls behavior when no active flow or task run context | |
| exists. `"raise"` (default) raises `MissingContextError` for backward | |
| compatibility. `"warn"` returns a standard `prefect` logger and emits a | |
| warning. `"ignore"` returns a standard `prefect` logger silently. Use | |
| `"warn"` or `"ignore"` when calling from threads or other contexts where | |
| a Prefect run context may not be available. |
| MissingContextError: If no context can be found and ``on_missing_context`` is | ||
| ``"raise"`` |
There was a problem hiding this comment.
| MissingContextError: If no context can be found and ``on_missing_context`` is | |
| ``"raise"`` | |
| MissingContextError: If no context can be found and `on_missing_context` is | |
| `"raise"` |
| logger = logging.getLogger("null") | ||
| logger.disabled = True | ||
| elif on_missing_context == "warn": | ||
| logger = get_logger("prefect.run_fallback") |
There was a problem hiding this comment.
I'd feel better about adding this if users had the option to customize this logger name.
| elif on_missing_context == "ignore": | ||
| logger = get_logger("prefect.run_fallback") | ||
| else: | ||
| raise MissingContextError("There is no active flow or task run context.") |
There was a problem hiding this comment.
It'd be good to mention the new on_missing_context kwarg in this exception message.
Apply docstring suggestions, add fallback_logger_name parameter for customizing the logger returned in warn/ignore modes, and mention on_missing_context in the MissingContextError message.
|
Friendly bump @desertaxle — all review feedback has been addressed and CI is green. Let me know if there's anything else needed! |
|
@zionts it looks like there are some test failures on this PR. Those need to be addressed before we can merge. |
|
This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. |
Summary
Closes #21027 —
get_run_logger()raisesMissingContextErrorin threaded contexts with no fallback option.When using
ThreadPoolExecutorinside Prefect tasks for parallel processing, worker threads don't inherit the Prefect run context. Any code that callsget_run_logger()in these threads crashes withMissingContextError. The only workaround today is to monkey-patchprefect.get_run_loggerglobally.Changes
New
on_missing_contextparameter onget_run_logger():"raise"(default): current behavior, fully backward compatible"warn": return a standardprefectlogger and emit a debug message"ignore": return a standardprefectlogger silentlyThe fallback logger is a standard
prefect.run_fallbacklogger created via the existingget_logger()function, so it inherits Prefect's logging configuration.Testing
tests/logging/test_on_missing_context.py"raise"behavior is unchanged (backward compat)"warn"returns logger and emits debug message"ignore"returns logger silentlyChecklist
<link to issue>"