Skip to content

Commit

Permalink
respect aliases when loading custom logging.yaml (#17162)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz authored Feb 20, 2025
1 parent 0888f84 commit f6d728a
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 110 deletions.
4 changes: 3 additions & 1 deletion src/prefect/logging/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def load_logging_config(path: Path) -> dict[str, Any]:
warnings.filterwarnings("ignore", category=DeprecationWarning)
config = yaml.safe_load(
# Substitute settings into the template in format $SETTING / ${SETTING}
template.substitute(current_settings.to_environment_variables())
template.substitute(
current_settings.to_environment_variables(include_aliases=True)
)
)

# Load overrides from the environment
Expand Down
21 changes: 18 additions & 3 deletions src/prefect/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def to_environment_variables(
self,
exclude_unset: bool = False,
include_secrets: bool = True,
include_aliases: bool = False,
) -> Dict[str, str]:
"""Convert the settings object to a dictionary of environment variables."""
env: Dict[str, Any] = self.model_dump(
Expand All @@ -105,12 +106,26 @@ def to_environment_variables(
child_env = child_settings.to_environment_variables(
exclude_unset=exclude_unset,
include_secrets=include_secrets,
include_aliases=include_aliases,
)
env_variables.update(child_env)
elif (value := env.get(key)) is not None:
env_variables[f"{self.model_config.get('env_prefix')}{key.upper()}"] = (
_to_environment_variable_value(value)
)
validation_alias = self.model_fields[key].validation_alias
if include_aliases and validation_alias is not None:
if isinstance(validation_alias, AliasChoices):
for alias in validation_alias.choices:
if isinstance(alias, str):
env_variables[alias.upper()] = (
_to_environment_variable_value(value)
)
elif isinstance(validation_alias, str):
env_variables[validation_alias.upper()] = (
_to_environment_variable_value(value)
)
else:
env_variables[
f"{self.model_config.get('env_prefix')}{key.upper()}"
] = _to_environment_variable_value(value)
return env_variables

@model_serializer(
Expand Down
Loading

0 comments on commit f6d728a

Please sign in to comment.