def _record_log(self, status_code, exc_info, **kwargs): params = _clean_credentials(self.request_data) exc = exc_info[1] if hasattr(exc, "message"): error_info = exc.message elif hasattr(exc, "messages"): error_info = exc.messages elif hasattr(exc, "detail"): error_info = exc.detail else: error_info = "" log_context = { "url": self.request.url, "method": self.request.method, "host": self.request.headers.get("Host", ""), "client_ip": self.request.client_ip(), "request_data": params, "http_status_code": status_code, "headers": { "user-agent": self.request.headers.get("User-Agent", ""), "content-type": self.request.headers.get("Content-Type", "") }, "time_zone": settings.TIME_ZONE, "time": timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f"), "error_info": error_info, } if kwargs: log_context.update(**kwargs) log_extend = self.get_log_extend() if log_extend and isinstance(log_extend, dict): log_context.update(**log_extend) log_context = json_encode(log_context) app_logger.error(log_context, exc_info=exc_info)
def render(self, content: typing.Any) -> bytes: if self.content_type == "application/json": return json_encode(content).encode(self.charset) if isinstance(content, bytes): return content return content.encode(self.charset)
def dumps(self, value): return force_bytes(json_encode(value))
async def send_json(self, data: typing.Any) -> None: send_data = json_encode(data).encode("utf-8") await self.send({"type": "websocket.send", "bytes": send_data})
def to_representation(self, value): if self.binary: value = json_encode(value) if isinstance(value, str): value = bytes(value.encode('utf-8')) return value
def data(self): return json_encode( self._data ) if self.content_type == "application/json" else self._data