a
    8öj°!  ã                   @   sª   d dl Z 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
 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)ÚIterable)ÚCallableÚClassVarÚDictÚListÚOptionalÚ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< dZeeej  ed< dZee 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j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d!œd1d2„ƒZ*eeeeed!œd3d4„ƒZ+eeeeed!œd5d6„ƒZ,eeee-eee.eeegef f  d!œd7d8„Z/e0e1d$œd9d:„ƒZ2eeedd!œd;d<„Z3‡  Z4S )=Ú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Ú
_cached_tzg        Ú_cached_tz_expires)ÚloggerÚ
log_formatÚreturnc                    sD   t ƒ j||d� tj |¡}|s4|  |¡}|tj|< |\| _| _dS )z©Initialise 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__© úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/aiohttp/web_log.pyr!   C   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#   S   s    zAccessLogger.compile_format)ÚkeyÚrequestÚresponseÚtimer   c                 C   s   |d u rdS |j  | d¡S )Nz(no headers)ú-©Úheadersr"   ©r9   r:   r;   r<   r)   r)   r*   Ú	_format_i|   s    zAccessLogger._format_ic                 C   s   |j  | d¡S ©Nr=   r>   r@   r)   r)   r*   Ú	_format_o†   s    zAccessLogger._format_o)r:   r;   r<   r   c                 C   s"   | d u rdS | j }|d ur|S dS rB   )Úremote)r:   r;   r<   Úipr)   r)   r*   Ú	_format_a�   s    zAccessLogger._format_a)r   c                 C   s�   | j d u st ¡ | jkr‚t ¡ j}t tj|d�¡ | _ }tj 	|¡}|tjdd� }|j
|jdkrhdndddd�}| ¡ | _|S tj 	| j ¡S )N©Úsecondsé   )Úminutesr   )ÚminuteÚsecondÚmicrosecond)r   Útime_modr<   r   Ú	localtimeÚ	tm_gmtoffÚdatetimeÚtimezoneÚ	timedeltaÚnowÚreplacerK   Ú	timestamp)ÚclsÚgmtoffÚtzrT   Údr)   r)   r*   Ú_get_local_time”   s    

zAccessLogger._get_local_timec                 C   s"   t  ¡ }|tj|d� }| d¡S )NrG   z[%d/%b/%Y:%H:%M:%S %z])r   r[   rQ   rS   Ústrftime)r:   r;   r<   rT   Ú
start_timer)   r)   r*   Ú	_format_t£   s    zAccessLogger._format_tc                 C   s   dt  ¡  S )Nz<%s>)ÚosÚgetpid©r:   r;   r<   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Úminorra   r)   r)   r*   Ú	_format_r­   s    üzAccessLogger._format_rc                 C   s   |j S ©N)Ústatusra   r)   r)   r*   Ú	_format_s¸   s    zAccessLogger._format_sc                 C   s   |j S ri   )Úbody_lengthra   r)   r)   r*   Ú	_format_b¼   s    zAccessLogger._format_bc                 C   s   t t|ƒƒS ri   ©ÚstrÚroundra   r)   r)   r*   Ú	_format_TÀ   s    zAccessLogger._format_Tc                 C   s   d| S )Nz%06fr)   ra   r)   r)   r*   Ú
_format_TfÄ   s    zAccessLogger._format_Tfc                 C   s   t t|d ƒƒS )Ni@B rn   ra   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)   )Ú.0r9   rd   ra   r)   r*   Ú
<listcomp>Ï   ó    z-AccessLogger._format_line.<locals>.<listcomp>)r%   )r&   r:   r;   r<   r)   ra   r*   Ú_format_lineÌ   s    zAccessLogger._format_linec                 C   s   | j  tj¡S )zCheck if logger is enabled.)r   ÚisEnabledForÚloggingÚINFO)r&   r)   r)   r*   ÚenabledÑ   s    zAccessLogger.enabledc                 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)rw   r-   Údictr4   r(   ro   r"   r   Úinfor$   ÚtupleÚ	ExceptionÚ	exception)r&   r:   r;   r<   Zfmt_infoÚvaluesr|   r9   ÚvalueZk1Zk2Údctr)   r)   r*   Úlog×   s    



zAccessLogger.log)5Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   Z
LOG_FORMATÚreÚcompiler.   r6   r   r   ro   r	   r   r   Ú__annotations__r   r   r   rQ   rR   r   Úfloatry   ÚLoggerr!   r#   Ústaticmethodr   r   rA   rC   rF   Úclassmethodr[   r^   rb   rh   Úintrk   rm   rq   rr   rs   r   r   rw   ÚpropertyÚboolr{   r…   Ú__classcell__r)   r)   r'   r*   r      sn   
õ

 )þ	þ
þr   )rQ   r2   ry   r_   rŠ   r<   rN   Úcollectionsr   Úcollections.abcr   Útypingr   r   r   r   r   r	   Úabcr   Zweb_requestr   Zweb_responser   r   r   r)   r)   r)   r*   Ú<module>   s    
