a
    }3e                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ edd	ZG d
d deZdS )    N)
namedtuple)AnyCallableDictIterableListTuple   )AbstractAccessLogger)BaseRequest)StreamResponse	KeyMethodz
key methodc                       s  e Zd ZU dZdddddddd	d
dddZdZedZedZ	i Z
eeeeee f f ed< efejedd fddZeeeee f dddZeeeeeedddZeeeeeedddZeeeeeddd Zeeeeedd!d"Zeeeeedd#d$Zeeeeedd%d&Zeeeeedd'd(Z eeeeedd)d*Z!eeeeedd+d,Z"eeeeedd-d.Z#eeeeedd/d0Z$eeee%eee&eeegef f  dd1d2Z'eeeddd3d4Z(  Z)S )5AccessLoggera  Helper 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']

    Zremote_addressZrequest_start_timeZ
process_idZfirst_request_lineZresponse_statusZresponse_sizeZrequest_timeZrequest_time_fracZrequest_time_microZrequest_headerZresponse_header)atPrsbTZTfDioz/%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"z.%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbOD]|Tf?)z(%[^s])_FORMAT_CACHEN)logger
log_formatreturnc                    sD   t  j||d tj|}|s4| |}|tj|< |\| _| _dS )zInitialise the logger.

        logger is a logger object to be used for logging.
        log_format is a string with apache compatible log format description.

        )r   N)super__init__r   r   getcompile_format_log_format_methods)selfr   r   Z_compiled_format	__class__ B/home/pi/bot/my_env/lib/python3.9/site-packages/aiohttp/web_log.pyr   >   s    

zAccessLogger.__init__)r   r   c                 C   s   t  }| j|D ]}|d dkrN| j|d  }ttd|d  }t||}n>| j|d  |d f}ttd|d  }t|t||d }|	| q| j
d|}| j
d|}||fS )a  Translate 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	    r   z
_format_%s   z%sz%\1)list	FORMAT_REfindallLOG_FORMAT_MAPgetattrr   r   	functoolspartialappendsub
CLEANUP_RE)r#   r   methodsZatomZformat_key1mZ
key_methodZformat_key2r&   r&   r'   r    N   s    zAccessLogger.compile_format)keyrequestresponsetimer   c                 C   s   |d u rdS |j | dS )Nz(no headers)-headersr   r6   r7   r8   r9   r&   r&   r'   	_format_iw   s    zAccessLogger._format_ic                 C   s   |j | dS Nr:   r;   r=   r&   r&   r'   	_format_o   s    zAccessLogger._format_o)r7   r8   r9   r   c                 C   s"   | d u rdS | j }|d ur|S dS r?   )remote)r7   r8   r9   ipr&   r&   r'   	_format_a   s    zAccessLogger._format_ac                 C   s$   t j  }|t j|d }|dS )N)secondsz[%d/%b/%Y:%H:%M:%S +0000])datetimeutcnow	timedeltastrftime)r7   r8   r9   now
start_timer&   r&   r'   	_format_t   s    
zAccessLogger._format_tc                 C   s   dt   S )Nz<%s>)osgetpidr7   r8   r9   r&   r&   r'   	_format_P   s    zAccessLogger._format_Pc                 C   s(   | d u rdS d | j| j| jj| jjS )Nr:   z{} {} HTTP/{}.{})formatmethodZpath_qsversionmajorminorrN   r&   r&   r'   	_format_r   s    zAccessLogger._format_rc                 C   s   |j S N)statusrN   r&   r&   r'   	_format_s   s    zAccessLogger._format_sc                 C   s   |j S rV   )body_lengthrN   r&   r&   r'   	_format_b   s    zAccessLogger._format_bc                 C   s   t t|S rV   strroundrN   r&   r&   r'   	_format_T   s    zAccessLogger._format_Tc                 C   s   d| S )Nz%06fr&   rN   r&   r&   r'   
_format_Tf   s    zAccessLogger._format_Tfc                 C   s   t t|d S )Ni@B r[   rN   r&   r&   r'   	_format_D   s    zAccessLogger._format_Dc                    s    fdd| j D S )Nc                    s    g | ]\}}|| fqS r&   r&   ).0r6   rQ   rN   r&   r'   
<listcomp>       z-AccessLogger._format_line.<locals>.<listcomp>)r"   )r#   r7   r8   r9   r&   rN   r'   _format_line   s    zAccessLogger._format_linec                 C   s   z|  |||}t }t }|D ]J\}}|| |jtu rF|||< q |\}	}
||	i }|||
< |||	< q | jj| j	t
| |d W n ty   | jd Y n0 d S )N)extrazError in logging)rd   r*   dictr1   r%   r\   r   r   infor!   tuple	Exception	exception)r#   r7   r8   r9   Zfmt_infovaluesre   r6   valueZk1Zk2dctr&   r&   r'   log   s    



zAccessLogger.log)*__name__
__module____qualname____doc__r-   Z
LOG_FORMATrecompiler+   r3   r   r   r\   r   r   r   __annotations__loggingLoggerr   r    staticmethodr   r   floatr>   r@   rC   rK   rO   rU   intrX   rZ   r^   r_   r`   r   r   rd   rn   __classcell__r&   r&   r$   r'   r      sb   


 )	
r   )rE   r/   rv   rL   rs   collectionsr   typingr   r   r   r   r   r   abcr
   Zweb_requestr   Zweb_responser   r   r   r&   r&   r&   r'   <module>   s    
