<s> """<STR_LIT>""" <EOL> import json <EOL> import traceback <EOL> from tornado . log import LogFormatter , access_log <EOL> from tornado . web import StaticFileHandler <EOL> def coroutine_traceback ( typ , value , tb ) : <EOL> """<STR_LIT>""" <EOL> all_frames = traceback . extract_tb ( tb ) <EOL> useful_frames = [ ] <EOL> for frame in all_frames : <EOL> if frame [ <NUM_LIT:0> ] == '<STR_LIT>' and frame [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> continue <EOL> elif frame [ <NUM_LIT:0> ] . endswith ( '<STR_LIT>' ) and frame [ <NUM_LIT:2> ] in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> continue <EOL> elif frame [ <NUM_LIT:0> ] . endswith ( '<STR_LIT>' ) and frame [ <NUM_LIT:2> ] == '<STR_LIT:result>' : <EOL> continue <EOL> useful_frames . append ( frame ) <EOL> tb_list = [ '<STR_LIT>' ] <EOL> tb_list . extend ( traceback . format_list ( useful_frames ) ) <EOL> tb_list . extend ( traceback . format_exception_only ( typ , value ) ) <EOL> return tb_list <EOL> class CoroutineLogFormatter ( LogFormatter ) : <EOL> """<STR_LIT>""" <EOL> def formatException ( self , exc_info ) : <EOL> return '<STR_LIT>' . join ( coroutine_traceback ( * exc_info ) ) <EOL> def _scrub_uri ( uri ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in uri or '<STR_LIT>' in uri : <EOL> uri = uri . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> return uri <EOL> def _scrub_headers ( headers ) : <EOL> """<STR_LIT>""" <EOL> headers = dict ( headers ) <EOL> if '<STR_LIT>' in headers : <EOL> auth = headers [ '<STR_LIT>' ] <EOL> if auth . startswith ( '<STR_LIT>' ) : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return headers <EOL> def log_request ( handler ) : <EOL> """<STR_LIT>""" <EOL> status = handler . get_status ( ) <EOL> request = handler . request <EOL> if status == <NUM_LIT> or ( status < <NUM_LIT> and isinstance ( handler , StaticFileHandler ) ) : <EOL> log_method = access_log . debug <EOL> elif status < <NUM_LIT> : <EOL> log_method = access_log . info <EOL> elif status < <NUM_LIT> : <EOL> log_method = access_log . warning <EOL> else : <EOL> log_method = access_log . error <EOL> uri = _scrub_uri ( request . uri ) <EOL> headers = _scrub_headers ( request . headers ) <EOL> request_time = <NUM_LIT> * handler . request . request_time ( ) <EOL> user = handler . get_current_user ( ) <EOL> ns = dict ( <EOL> status = status , <EOL> method = request . method , <EOL> ip = request . remote_ip , <EOL> uri = uri , <EOL> request_time = request_time , <EOL> user = user . name if user else '<STR_LIT>' <EOL> ) <EOL> msg = "<STR_LIT>" <EOL> if status >= <NUM_LIT> and status != <NUM_LIT> : <EOL> log_method ( json . dumps ( headers , indent = <NUM_LIT:2> ) ) <EOL> log_method ( msg . format ( ** ns ) ) </s>
