logging.py 文件源码

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

项目:python-dockerflow 作者: mozilla-services 项目源码 文件源码
def format(self, record):
        """
        Map from Python LogRecord attributes to JSON log format fields

        * from - https://docs.python.org/3/library/logging.html#logrecord-attributes
        * to - https://mana.mozilla.org/wiki/pages/viewpage.action?pageId=42895640
        """
        out = dict(
            Timestamp=int(record.created * 1e9),
            Type=record.name,
            Logger=self.logger_name,
            Hostname=self.hostname,
            EnvVersion=self.LOGGING_FORMAT_VERSION,
            Severity=self.SYSLOG_LEVEL_MAP.get(record.levelno,
                                               self.DEFAULT_SYSLOG_LEVEL),
            Pid=record.process,
        )

        # Include any custom attributes set on the record.
        # These would usually be collected metrics data.
        fields = dict()
        for key, value in record.__dict__.items():
            if key not in self.EXCLUDED_LOGRECORD_ATTRS:
                fields[key] = value

        # Only include the 'msg' key if it has useful content
        # and is not already a JSON blob.
        message = record.getMessage()
        if message and not message.startswith('{') and not message.endswith('}'):
            fields['msg'] = message

        # If there is an error, format it for nice output.
        if record.exc_info is not None:
            fields['error'] = repr(record.exc_info[1])
            fields['traceback'] = safer_format_traceback(*record.exc_info)

        out['Fields'] = fields

        return json.dumps(out, cls=SafeJSONEncoder)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号