-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8.py
50 lines (36 loc) · 1.19 KB
/
v8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging.config
import time
import uuid
from functools import wraps
from flask import Flask, g
from flask.views import MethodView
from common import get_logging_config
logging.config.dictConfig(get_logging_config(['context']))
def global_error_handler(e):
app.logger.error(str(e), exc_info=e)
return 'unexpected error', 500
def add_request_id():
def decorator(f):
@wraps(f)
def decorated_function(self):
request_id = uuid.uuid4()
g.log_context = {
'request_id': request_id,
'view': self.__class__.__name__,
}
app.logger.info(f'hello from middleware ({request_id})')
return f(self, request_id)
return decorated_function
return decorator
class HelloView(MethodView):
@add_request_id()
def get(self, request_id):
time.sleep(0.01)
app.logger.info(f'hello from view ({request_id})')
raise Exception('oops')
app = Flask(__name__)
app.add_url_rule('/', view_func=HelloView.as_view('hello'))
app.register_error_handler(404, lambda _: ('', 404))
app.register_error_handler(Exception, global_error_handler)
if __name__ == '__main__':
app.run()