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

CustomTTYColoredFormatter should inherit TimezoneAware formatter #28439

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
4 changes: 3 additions & 1 deletion airflow/utils/log/colored_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from colorlog import TTYColoredFormatter
from colorlog.escape_codes import esc, escape_codes

from airflow.utils.log.timezone_aware import TimezoneAware

DEFAULT_COLORS = {
"DEBUG": "green",
"INFO": "",
Expand All @@ -38,7 +40,7 @@
BOLD_OFF = esc("22")


class CustomTTYColoredFormatter(TTYColoredFormatter):
class CustomTTYColoredFormatter(TTYColoredFormatter, TimezoneAware):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to add any tests for it :) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh forgot to add... sec

"""
Custom log formatter which extends `colored.TTYColoredFormatter`
by adding attributes to message arguments and coloring error
Expand Down
36 changes: 36 additions & 0 deletions tests/utils/log/test_colored_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

import logging
from unittest.mock import patch

from airflow.utils.log.colored_log import CustomTTYColoredFormatter


@patch("airflow.utils.log.timezone_aware.TimezoneAware.formatTime")
def test_format_time_uses_tz_aware(mock_fmt):
# get a logger that uses CustomTTYColoredFormatter
logger = logging.getLogger("test_format_time")
h = logging.StreamHandler()
h.setFormatter(CustomTTYColoredFormatter())
logger.addHandler(h)

# verify that it uses TimezoneAware.formatTime
logger.info("hi")
mock_fmt.assert_called()