U '7`J@sddlZddlZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z m Z ddlmZddlmZddlmZedd ZGd d d eZdS) N) namedtuple)AnyCallableDictIterableListTuple)AbstractAccessLogger) BaseRequest)StreamResponse KeyMethodz key methodc seZdZdZdddddddd d d d d ZdZedZedZ iZ efe j e ddfdd Ze ee eefdddZee eeee dddZee eeee dddZeeeee dddZeeeee dd d!Zeeeee dd"d#Zeeeee dd$d%Zeeeeedd&d'Zeeeeedd(d)Zeeeee dd*d+Z eeeee dd,d-Z!eeeee dd.d/Z"eeee#ee e$eeege ffdd0d1Z%eeeddd2d3Z&Z'S)4 AccessLoggeraHelper object to log access. Usage: log = logging.getLogger("spam") log_format = "%a %{User-Agent}i" access_logger = AccessLogger(log, log_format) access_logger.log(request, response, time) Format: %% The percent sign %a Remote IP-address (IP-address of proxy if using reverse proxy) %t Time when the request was started to process %P The process ID of the child that serviced the request %r First line of request %s Response status code %b Size of response in bytes, including HTTP headers %T Time taken to serve the request, in seconds %Tf Time taken to serve the request, in seconds with floating fraction in .06f format %D Time taken to serve the request, in microseconds %{FOO}i request.headers['FOO'] %{FOO}o response.headers['FOO'] %{FOO}e os.environ['FOO'] Zremote_addressZrequest_start_timeZ process_idZfirst_request_lineZresponse_statusZ response_sizeZ request_timeZrequest_time_fracZrequest_time_microZrequest_headerZresponse_header) atPrsbTZTfDioz/%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"z.%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbOD]|Tf?)z(%[^s])N)logger log_formatreturncsDtj||dtj|}|s4||}|tj|<|\|_|_dS)zInitialise the logger. logger is a logger object to be used for logging. log_format is a string with apache compatible log format description. )rN)super__init__r _FORMAT_CACHEgetcompile_format _log_format_methods)selfrrZ_compiled_format __class__B/opt/alt/python38/lib64/python3.8/site-packages/aiohttp/web_log.pyr>s    zAccessLogger.__init__)rrcCst}|j|D]}|ddkrN|j|d}ttd|d}t||}n>|j|d|df}ttd|d}t|t||d}| |q|j d|}|j d|}||fS)aTranslate log_format into form usable by modulo formatting All known atoms will be replaced with %s Also methods for formatting of those atoms will be added to _methods in appropriate order For example we have log_format = "%a %t" This format will be translated to "%s %s" Also contents of _methods will be [self._format_a, self._format_t] These method will be called and results will be passed to translated string format. Each _format_* method receive 'args' which is list of arguments given to self.log Exceptions are _format_e, _format_i and _format_o methods which also receive key name (by functools.partial) r rz _format_%sz%sz%\1) list FORMAT_REfindallLOG_FORMAT_MAPgetattrrr functoolspartialappendsub CLEANUP_RE)r#rmethodsZatomZ format_key1mZ key_methodZ format_key2r&r&r'r Ns   zAccessLogger.compile_format)keyrequestresponsetimercCs|dkr dS|j|dS)Nz (no headers)-Zheadersrr6r7r8r9r&r&r' _format_iwszAccessLogger._format_icCs|j|dSNr:r;r<r&r&r' _format_oszAccessLogger._format_o)r7r8r9rcCs"|dkr dS|j}|dk r|SdSr>)Zremote)r7r8r9Zipr&r&r' _format_aszAccessLogger._format_acCs$tj}|tj|d}|dS)N)Zsecondsz[%d/%b/%Y:%H:%M:%S +0000])datetimeZutcnowZ timedeltastrftime)r7r8r9ZnowZ start_timer&r&r' _format_ts zAccessLogger._format_tcCs dtS)Nz<%s>)osgetpidr7r8r9r&r&r' _format_PszAccessLogger._format_PcCs(|dkr dSd|j|j|jj|jjS)Nr:z{} {} HTTP/{}.{})formatmethodZpath_qsversionmajorminorrFr&r&r' _format_rszAccessLogger._format_rcCs|jSN)ZstatusrFr&r&r' _format_sszAccessLogger._format_scCs|jSrN)Z body_lengthrFr&r&r' _format_bszAccessLogger._format_bcCs tt|SrNstrroundrFr&r&r' _format_TszAccessLogger._format_TcCsd|S)Nz%06fr&rFr&r&r' _format_TfszAccessLogger._format_TfcCstt|dS)Ni@BrQrFr&r&r' _format_DszAccessLogger._format_Dcsfdd|jDS)Ncs g|]\}}||fqSr&r&).0r6rIrFr&r' sz-AccessLogger._format_line..)r")r#r7r8r9r&rFr' _format_lineszAccessLogger._format_linec Csz||||}t}t}|D]J\}}|||jtkrF|||<q |\} } || i} || | <| || <q |jj|j t ||dWn t k r|j dYnXdS)N)extrazError in logging) rYr*dictr1r%rRrrinfor!tuple ExceptionZ exception) r#r7r8r9Zfmt_infovaluesrZr6valueZk1Zk2Zdctr&r&r'logs      zAccessLogger.log)(__name__ __module__ __qualname____doc__r-Z LOG_FORMATrecompiler+r3rloggingZLoggerrRrrrr r staticmethodr r floatr=r?r@rCrGrMintrOrPrTrUrVrrrYra __classcell__r&r&r$r'rsr  )   r)rAr/rhrDrf collectionsrtypingrrrrrrabcr Z web_requestr Z web_responser r rr&r&r&r's