mixin有2个使用原则:
1. 如果你想给一个类附加很多可选的方法和属性
2. 有很多类使用同一个特定的方法和属性
一个比较好的在类的方法里添加日志的mixin
class LoggingMixin(object):
"""
Convenience super-class to have a logger configured with the class name
"""
def __init__(self, context=None):
if context is not None:
set_context(self.log, context)
# We want to deprecate the logger property in Airflow 2.0
# The log property is the de facto standard in most programming languages
@property
def logger(self):
warnings.warn(
'Initializing logger for {} using logger(), which will '
'be replaced by .log in Airflow 2.0'.format(
self.__class__.__module__ + '.' + self.__class__.__name__
),
DeprecationWarning
)
return self.log
@property
def log(self):
try:
return self._log
except AttributeError:
self._log = logging.root.getChild(
self.__class__.__module__ + '.' + self.__class__.__name__
)
return self._log