flask的signals模块

flask的signals模块中基于blinker信号量库,创建了request_started、request_finished、got_request_exception等Signal对象。flask在相关事件发生时,就调用对应Signal对象发布信号。

下面以got_request_exception举例:

class Flask(_PackageBoundObject):
	...
	
	def handle_exception(self, e):
	"""Default exception handling that kicks in when an exception
	occurs that is not caught.  In debug mode the exception will
	be re-raised immediately, otherwise it is logged and the handler
	for a 500 internal server error is used.  If no such handler
	exists, a default 500 internal server error message is displayed.

	.. versionadded:: 0.3
	"""
	exc_type, exc_value, tb = sys.exc_info()

	got_request_exception.send(self, exception=e)
	handler = self._find_error_handler(InternalServerError())

	if self.propagate_exceptions:
		# if we want to repropagate the exception, we can attempt to
		# raise it with the whole traceback in case we can do that
		# (the function was actually called from the except part)
		# otherwise, we just raise the error again
		if exc_value is e:
			reraise(exc_type, exc_value, tb)
		else:
			raise e

	self.log_exception((exc_type, exc_value, tb))
	if handler is None:
		return InternalServerError()
	return self.finalize_request(handler(e), from_error_handler=True)
	
	...

每当异常发生时,flask都通过got_request_exception.send(self, exception=e)发布异常信号,所以我们可以订阅对应异常信号,当信号发布时,进行日志记录。例如:

@got_request_exception.connect_via(app)
def log_exception(sender, exception, **extra):
    sender.logger.info('{remote_host} -- "{method} {path} {protocol}" - {user_agent}'.format(
        remote_host=request.remote_addr,
        method=request.method,
        protocol=request.headers.environ['SERVER_PROTOCOL'],
        path=request.full_path,
        user_agent=request.user_agent))

    if request.method != "GET":
        sender.logger.debug(request.json)

    if not isinstance(exception, HTTPException):
        sender.logger.exception(exception)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值