3535
3636app = connexion .App (__name__ , specification_dir = './openapi/' )
3737app .app .json_encoder = encoder .JSONEncoder
38- api_blueprint = app .add_api ('openapi.yaml' ,
39- arguments = {'title' : 'Mist API' },
40- pythonic_params = True )
38+ app .add_api ('openapi.yaml' ,
39+ arguments = {'title' : 'Mist API' },
40+ pythonic_params = True )
4141application = app .app
4242
4343logging .basicConfig (level = config .PY_LOG_LEVEL ,
@@ -62,18 +62,17 @@ def prepare_log(response):
6262 g .exc_flag is False ):
6363 return response
6464
65+ # Find the action from flask's dict mapping endpoints to view functions.
6566 try :
66- operation = api_blueprint .specification .get_operation (
67- request .path , request .method .lower ())
68- operation_id = operation ['operationId' ]
67+ action = application .view_functions [request .endpoint ].__name__
6968 except KeyError :
70- log .error ('Could not find operation id . Path:%s Method:%s' ,
69+ log .error ('Could not find action . Path:%s Method:%s' ,
7170 request .path , request .method )
7271 return response
7372
7473 log_dict = {
7574 'event_type' : 'request' ,
76- 'action' : operation_id ,
75+ 'action' : action ,
7776 'request_path' : request .path ,
7877 'request_method' : request .method ,
7978 'request_ip' : request .environ ['HTTP_X_REAL_IP' ],
@@ -194,10 +193,10 @@ def log_request_to_elastic(exception):
194193 # elastic
195194 log .info ('Bad exception occured, logging to rabbitmq' )
196195 es_dict = log_dict .copy ()
197- es_dict .pop ('_exc_type' )
196+ es_dict .pop ('_exc_type' , '' )
198197 es_dict ['time' ] = time .time ()
199198 es_dict ['traceback' ] = es_dict .pop ('_traceback' , '' )
200- es_dict ['exception' ] = es_dict .pop ('_exc' )
199+ es_dict ['exception' ] = es_dict .pop ('_exc' , '' )
201200 es_dict ['type' ] = 'exception'
202201 routing_key = '%s.%s' % (es_dict ['owner_id' ], es_dict ['action' ])
203202 pickler = jsonpickle .pickler .Pickler ()
@@ -208,8 +207,8 @@ def log_request_to_elastic(exception):
208207 # log bad exception to file
209208 log .info ('Bad exception occured, logging to file' )
210209 lines = []
211- lines .append ('Exception: %s' % log_dict .pop ('_exc' ))
212- lines .append ('Exception type: %s' % log_dict .pop ('_exc_type' ))
210+ lines .append ('Exception: %s' % log_dict .pop ('_exc' , '' ))
211+ lines .append ('Exception type: %s' % log_dict .pop ('_exc_type' , '' ))
213212 lines .append ('Time: %s' % time .strftime ('%Y-%m-%d %H:%M %Z' ))
214213 lines += (
215214 ['%s: %s' % (key , value ) for key , value in list (log_dict .items ())
0 commit comments