logger.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:ml-utils 作者: LinxiFan 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号