You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/07/29 16:10:21 UTC

[GitHub] [airflow] BasPH commented on a change in pull request #5681: [AIRFLOW-5065] Add colors to console log

BasPH commented on a change in pull request #5681: [AIRFLOW-5065] Add colors to console log
URL: https://github.com/apache/airflow/pull/5681#discussion_r308312265
 
 

 ##########
 File path: airflow/utils/log/colored_log.py
 ##########
 @@ -0,0 +1,117 @@
+# -*- coding: utf-8 -*-
+#
+# 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.
+"""
+This module contains ColorFormatter.
+"""
+
+from typing import Any, Union
+from logging import Formatter, LogRecord
+
+from termcolor import colored
+
+RED = "red"
+BLUE = "blue"
+GREEN = "green"
+YELLOW = "yellow"
+CYAN = "cyan"
+MAGENTA = "magenta"
+
+ARGS = {"attrs": ["bold"]}
+TIME_COLOR = BLUE
+FILENAME_COLOR = BLUE
+ERROR = RED
+WARN = YELLOW
+
+
+def color_arg(arg: Any) -> Union[str, float, int]:
+    """
+    Helper for coloring arguments.
+    """
+    if isinstance(arg, (int, float)):
+        # In case of %d or %f formatting
+        return arg
+    return colored(str(arg), **ARGS)  # type: ignore
+
+
+class ColorFormatter(Formatter):
+    """
+    This class extends logging.Formatter by providing
+    a custom format method that adds colors to log records.
+    """
+
+    @staticmethod
+    def _format_record(record: LogRecord) -> LogRecord:
+        """
+        Adds colors to log record.
+
+        :param record: An log record.
+        :type record: logging.LogRecord
+        :return: Colored LogRecord.
+        """
+        record.filename = colored(record.filename, FILENAME_COLOR)
+
+        # Color level info
+        if record.levelname in ("ERROR", "DEBUG", "CRITICAL"):
+            record.msg = colored(record.msg, ERROR)
 
 Review comment:
   Here you apply a mapping from ERROR, DEBUG and CRITICAL to the ERROR color. For clarity sake, I'd avoid the logic hidden in if-else conditions but instead create a dict which maps log levels to colors, e.g.:
   
   ```python
   log_level_color_mapping = {
       "ERROR": RED,
       "DEBUG": RED,
       ...
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services