logger.py 文件源码

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

项目:two1-python 作者: 21dotco 项目源码 文件源码
def emit(self, record):
        """ Echos the record.msg string by using click.echo()

            The record will have attributes set to it when a user logs a message
            with any kwargs given. This function looks for any attributes that
            are in ECHO_KWARGS. If record has any kwargs attributes, the record will
            be echoed with the given kwargs. This makes click logging with a logger very easy.

            A user can add {"pager": True} as an extra param to any of the log commands to print
            the content to a pager.

        Args:
            record (logging.LogRecord): record which gets echoed with click.echo()
        """
        try:
            # first format the record which adds the click style
            formatted_record = self.format(record)

            # user wants to use a pager to show message
            if hasattr(record, "pager") and record.pager:
                # save the original eviron dict
                original_env = dict(os.environ)

                # Force system to use pager and add default prompt at bottom left of the screen
                os.environ['PAGER'] = "less"
                os.environ['LESS'] = LESS_ENV
                try:
                    click.echo_via_pager(formatted_record.msg,
                                         color=record.color if hasattr(record, "color") else None)

                # being paranoid here because we do NOT want to mess with people's environment
                except:
                    os.environ.clear()
                    os.environ.update(original_env)
                    raise
                else:
                    os.environ.clear()
                    os.environ.update(original_env)
            else:
                # Sets the default kwargs
                kwargs = dict()

                # if user added a known kwarg, change the defaults
                for kwarg_name in self.ECHO_KWARGS:
                    if hasattr(record, kwarg_name):
                        kwargs[kwarg_name] = getattr(record, kwarg_name)

                # echo to console
                click.echo(formatted_record.msg, **kwargs)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号