Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TaskHandlerWithCustomFormatter now adds prefix only once #38502

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/config_templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ logging:
Specify prefix pattern like mentioned below with stream handler TaskHandlerWithCustomFormatter
version_added: 2.0.0
type: string
example: "{ti.dag_id}-{ti.task_id}-{execution_date}-{try_number}"
example: "{{ti.dag_id}}-{{ti.task_id}}-{{execution_date}}-{{ti.try_number}}"
is_template: true
default: ""
log_filename_template:
Expand Down
3 changes: 2 additions & 1 deletion airflow/utils/log/task_handler_with_custom_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def set_context(self, ti) -> None:
:param ti:
:return:
"""
if ti.raw or self.formatter is None:
# Returns if there is no formatter or if the prefix has already been set
if ti.raw or self.formatter is None or self.prefix_jinja_template is not None:
return
prefix = conf.get("logging", "task_log_prefix_template")

Expand Down
24 changes: 20 additions & 4 deletions tests/utils/test_task_handler_with_custom_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,36 @@ def task_instance():
clear_db_runs()


def assert_prefix(task_instance: TaskInstance, prefix: str) -> None:
def assert_prefix_once(task_instance: TaskInstance, prefix: str) -> None:
handler = next((h for h in task_instance.log.handlers if h.name == TASK_HANDLER), None)
assert handler is not None, "custom task log handler not set up correctly"
assert handler.formatter is not None, "custom task log formatter not set up correctly"
previous_formatter = handler.formatter
expected_format = f"{prefix}:{handler.formatter._fmt}"
set_context(task_instance.log, task_instance)
assert expected_format == handler.formatter._fmt
handler.setFormatter(previous_formatter)


def assert_prefix_multiple(task_instance: TaskInstance, prefix: str) -> None:
handler = next((h for h in task_instance.log.handlers if h.name == TASK_HANDLER), None)
assert handler is not None, "custom task log handler not set up correctly"
assert handler.formatter is not None, "custom task log formatter not set up correctly"
previous_formatter = handler.formatter
expected_format = f"{prefix}:{handler.formatter._fmt}"
set_context(task_instance.log, task_instance)
set_context(task_instance.log, task_instance)
set_context(task_instance.log, task_instance)
assert expected_format == handler.formatter._fmt
handler.setFormatter(previous_formatter)


def test_custom_formatter_default_format(task_instance):
"""The default format provides no prefix."""
assert_prefix(task_instance, "")
assert_prefix_once(task_instance, "")


@conf_vars({("logging", "task_log_prefix_template"): "{{ti.dag_id }}-{{ ti.task_id }}"})
@conf_vars({("logging", "task_log_prefix_template"): "{{ ti.dag_id }}-{{ ti.task_id }}"})
def test_custom_formatter_custom_format_not_affected_by_config(task_instance):
assert_prefix(task_instance, f"{DAG_ID}-{TASK_ID}")
"""Certifies that the prefix is only added once, even after repeated calls"""
assert_prefix_multiple(task_instance, f"{DAG_ID}-{TASK_ID}")