B `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__j/private/var/folders/7j/8686xlfs15q3tgljmghtvg0r0000gn/T/pip-target-isidps9b/lib/python/aiohttp/web_log.pyr>s    zAccessLogger.__init__)rrcCst}x|j|D]}|ddkrP|j|d}ttd|d}t||}n>|j|d|df}ttd|d}t|t||d}| |qW|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)-)headersr)r5r6r7r8r%r%r& _format_iwszAccessLogger._format_icCs|j|dS)Nr9)r:r)r5r6r7r8r%r%r& _format_oszAccessLogger._format_o)r6r7r8rcCs"|dkr dS|j}|dk r|SdS)Nr9)remote)r6r7r8ipr%r%r& _format_aszAccessLogger._format_acCs$tj}|tj|d}|dS)N)secondsz[%d/%b/%Y:%H:%M:%S +0000])datetimeutcnow timedeltastrftime)r6r7r8now start_timer%r%r& _format_ts zAccessLogger._format_tcCs dtS)Nz<%s>)osgetpid)r6r7r8r%r%r& _format_PszAccessLogger._format_PcCs(|dkr dSd|j|j|jj|jjS)Nr9z{} {} HTTP/{}.{})formatmethodZpath_qsversionmajorminor)r6r7r8r%r%r& _format_rszAccessLogger._format_rcCs|jS)N)status)r6r7r8r%r%r& _format_sszAccessLogger._format_scCs|jS)N) body_length)r6r7r8r%r%r& _format_bszAccessLogger._format_bcCs tt|S)N)strround)r6r7r8r%r%r& _format_TszAccessLogger._format_TcCsd|S)Nz%06fr%)r6r7r8r%r%r& _format_TfszAccessLogger._format_TfcCstt|dS)Ni@B)rUrV)r6r7r8r%r%r& _format_DszAccessLogger._format_Dcsfdd|jDS)Ncs g|]\}}||fqSr%r%).0r5rL)r6r7r8r%r& sz-AccessLogger._format_line..)r")r#r6r7r8r%)r6r7r8r& _format_lineszAccessLogger._format_linec Csy||||}t}t}xR|D]J\}}|||jtkrH|||<q"|\} } || i} || | <| || <q"W|jj|j t ||dWn t k r|j dYnXdS)N)extrazError in logging) r\r)dictr0r$rUrrinfor!tuple Exception exception) r#r6r7r8Zfmt_infovaluesr]r5valueZk1Zk2dctr%r%r&logs     zAccessLogger.log)(__name__ __module__ __qualname____doc__r,Z LOG_FORMATrecompiler*r2rloggingLoggerrUrrrr r staticmethodr r floatr;r<r?rGrJrPintrRrTrWrXrYrrr\rf __classcell__r%r%)r$r&rsZ  ) (r)rAr.rmrHrk collectionsrtypingrrrrrrabcr Z web_requestr Z web_responser r rr%r%r%r&s