logging_filters.py 文件源码

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

项目:django-requestlogging 作者: tarkatronic 项目源码 文件源码
def filter(self, record):
        """
        Adds information from the request to the logging *record*.

        If certain information cannot be extracted from ``self.request``,
        a hyphen ``'-'`` is substituted as a placeholder.
        """
        request = self.request
        # Basic
        record.request_method = getattr(request, 'method', '-')
        record.path_info = getattr(request, 'path_info', '-')
        # User
        user = getattr(request, 'user', None)
        if django.VERSION < (1, 10) and user and not user.is_anonymous():
            record.username = user.username
        elif django.VERSION > (1, 10) and user and not user.is_anonymous:
            record.username = user.username
        else:
            record.username = '-'
        # Headers
        META = getattr(request, 'META', {})  # NOQA: N806
        record.remote_addr = META.get('REMOTE_ADDR', '-')
        record.server_protocol = META.get('SERVER_PROTOCOL', '-')
        record.http_user_agent = META.get('HTTP_USER_AGENT', '-')
        return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号