def set_formatter(self, formatter):
"""
Sets a custom formatter for *all* handlers.
https://docs.python.org/3/library/logging.html#formatter-objects
Args:
formatter: can be either of the following:
- instance of logging.Formatter
- tuple of fmt strings (fmt, datefmt), note that the style is `{}`
References:
- for fmt string:
https://docs.python.org/3/library/logging.html#logrecord-attributes
- for datefmt string:
https://docs.python.org/3/library/time.html#time.strftime
"""
if isinstance(formatter, (list, tuple)):
assert len(formatter) == 2, 'formatter=(fmt, datefmt) strings'
fmt, datefmt = formatter
datefmt = self.get_datefmt(datefmt)
formatter = logging.Formatter(fmt, datefmt, style='{')
elif not isinstance(formatter, logging.Formatter):
raise TypeError('formatter must be either an instance of logging.Formatter'
' or a tuple of (fmt, datefmt) strings')
for handler in self.logger.handlers:
handler.setFormatter(formatter)
评论列表
文章目录