File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 3737from prefect .client .schemas .objects import RunType
3838from prefect .events .worker import EventsWorker
3939from prefect .exceptions import MissingContextError
40+ from prefect .logging .configuration import ensure_logging_setup
4041from prefect .results import (
4142 ResultStore ,
4243 get_default_persist_setting ,
@@ -163,6 +164,8 @@ def hydrated_context(
163164
164165 with ExitStack () as stack :
165166 if serialized_context :
167+ ensure_logging_setup ()
168+
166169 # Set up settings context
167170 if settings_context := serialized_context .get ("settings_context" ):
168171 stack .enter_context (SettingsContext (** settings_context ))
Original file line number Diff line number Diff line change @@ -65,6 +65,19 @@ def load_logging_config(path: Path) -> dict[str, Any]:
6565 return flatdict_to_dict (flat_config )
6666
6767
68+ def ensure_logging_setup () -> None :
69+ """
70+ Ensure Prefect logging is configured in this process, calling
71+ `setup_logging` only if it has not already been called.
72+
73+ Use this in remote execution environments (e.g. Dask/Ray workers) where
74+ the normal SDK entry point (`import prefect`) may not have triggered
75+ logging configuration.
76+ """
77+ if not PROCESS_LOGGING_CONFIG :
78+ setup_logging ()
79+
80+
6881def setup_logging (incremental : bool | None = None ) -> dict [str , Any ]:
6982 """
7083 Sets up logging.
Original file line number Diff line number Diff line change 3333from prefect .logging import LogEavesdropper
3434from prefect .logging .configuration import (
3535 DEFAULT_LOGGING_SETTINGS_PATH ,
36+ ensure_logging_setup ,
3637 load_logging_config ,
3738 setup_logging ,
3839)
@@ -285,6 +286,22 @@ def test_setup_logging_applies_root_config_when_no_prior_configuration(
285286 assert called_config ["root" ]["handlers" ] == ["console" ]
286287
287288
289+ def test_ensure_logging_setup_calls_setup_logging_when_not_configured (
290+ dictConfigMock : MagicMock ,
291+ ):
292+ ensure_logging_setup ()
293+ dictConfigMock .assert_called_once ()
294+
295+
296+ def test_ensure_logging_setup_is_idempotent (dictConfigMock : MagicMock ):
297+ ensure_logging_setup ()
298+ ensure_logging_setup ()
299+ ensure_logging_setup ()
300+ # setup_logging should only be called once since PROCESS_LOGGING_CONFIG
301+ # is populated after the first call
302+ dictConfigMock .assert_called_once ()
303+
304+
288305def test_setting_aliases_respected_for_logging_config (tmp_path : Path ):
289306 logging_config_content = """
290307loggers:
You can’t perform that action at this time.
0 commit comments