You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ur...@apache.org on 2021/09/06 08:17:17 UTC

[airflow] branch main updated: Opmitise LoggingMixin.log (#17280)

This is an automated email from the ASF dual-hosted git repository.

uranusjr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new fe7efca  Opmitise LoggingMixin.log (#17280)
fe7efca is described below

commit fe7efcaeb22f9053aa1cfdec01a50dc943583a8e
Author: Tzu-ping Chung <tp...@astronomer.io>
AuthorDate: Mon Sep 6 16:16:44 2021 +0800

    Opmitise LoggingMixin.log (#17280)
    
    The log instance is now instantiated per class, instead of per instance,
    saving duplicated getLogger() calls. Those getLogger() calls would
    return the same logger instance anyway, so the functionality is
    effectively the same; only the function call overhead os saved.
    
    The self.log property is also converted to a cached_property, so the
    'is None' check would only happen once per instance, instead of every
    time the property is accessed.
---
 airflow/utils/log/logging_mixin.py | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/airflow/utils/log/logging_mixin.py b/airflow/utils/log/logging_mixin.py
index 1fc0757..bc67e2d 100644
--- a/airflow/utils/log/logging_mixin.py
+++ b/airflow/utils/log/logging_mixin.py
@@ -16,10 +16,12 @@
 # specific language governing permissions and limitations
 # under the License.
 import abc
-import logging
 import re
 import sys
-from logging import Handler, Logger, StreamHandler
+from logging import Handler, Logger, StreamHandler, getLogger
+from typing import Optional
+
+from airflow.compat.functools import cached_property
 
 # 7-bit C1 ANSI escape sequences
 ANSI_ESCAPE = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]')
@@ -36,18 +38,19 @@ def remove_escape_codes(text: str) -> str:
 class LoggingMixin:
     """Convenience super-class to have a logger configured with the class name"""
 
+    _log: Optional[Logger] = None
+
     def __init__(self, context=None):
         self._set_context(context)
 
-    @property
+    @cached_property
     def log(self) -> Logger:
         """Returns a logger."""
-        try:
-            # FIXME: LoggingMixin should have a default _log field.
-            return self._log  # type: ignore
-        except AttributeError:
-            self._log = logging.getLogger(self.__class__.__module__ + '.' + self.__class__.__name__)
-            return self._log
+        if self.__class__._log is None:
+            logger = getLogger(self.__class__.__module__ + '.' + self.__class__.__name__)
+            self.__class__._log = logger
+            return logger
+        return self.__class__._log
 
     def _set_context(self, context):
         if context is not None: