本文整理汇总了Python中flask.g.start方法的典型用法代码示例。如果您正苦于以下问题:Python g.start方法的具体用法?Python g.start怎么用?Python g.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.g
的用法示例。
在下文中一共展示了g.start方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_app
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import start [as 别名]
def init_app(app):
@app.before_request
def record_request_details():
CONCURRENT_REQUESTS.inc()
g.start = monotonic()
g.endpoint = request.endpoint
@app.after_request
def after_request(response):
CONCURRENT_REQUESTS.dec()
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
@app.errorhandler(Exception)
def exception(error):
app.logger.exception(error)
# error.code is set for our exception types.
msg = getattr(error, 'message', str(error))
code = getattr(error, 'code', 500)
return jsonify(result='error', message=msg), code
@app.errorhandler(WerkzeugHTTPException)
def werkzeug_exception(e):
return make_response(
jsonify(result='error', message=e.description),
e.code,
e.get_headers()
)
@app.errorhandler(404)
def page_not_found(e):
msg = e.description or "Not found"
return jsonify(result='error', message=msg), 404
示例2: pre_request
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import start [as 别名]
def pre_request():
global database
database.db.initializeSession()
g.start = time.time()
示例3: after_request
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import start [as 别名]
def after_request(response):
global LOGGER
timediff = time.time() - g.start
debug and LOGGER.warn("Exec time: {}".format(str(timediff)))
return response
示例4: init_app
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import start [as 别名]
def init_app(app):
def start_timer():
request_id = request.headers.get("X-Request-ID", str(uuid4()))
g.logger = current_app.logger.bind(request_id=request_id)
g.request_id = request_id
g.start = datetime.now()
app.before_request(start_timer)
def log_request(response):
if request.path == "/favicon.ico":
return response
now = datetime.now()
if hasattr(g, "start"):
duration = int(round((now - g.start).microseconds / 1000, 2))
else:
duration = -1
user_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
host = request.host.split(":", 1)[0]
log_params = {
"method": request.method,
"path": request.path,
"status": response.status_code,
"duration": duration,
"ip": user_ip,
"host": host,
}
request_id = getattr(g, "request_id", None)
if request_id:
log_params["request_id"] = request_id
if response.status_code < 400:
current_app.logger.info("Request succeeded", **log_params)
elif response.status_code < 500:
current_app.logger.info("Bad Request", **log_params)
else:
current_app.logger.error("Internal Server Error", **log_params)
return response
app.after_request(log_request)