a
    ø£Be­  ã                   @   sd   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
 h d£Zeed	œd
d„Zdd„ ZdS )zLog utilities.é    N)ÚurlparseÚ
urlunparse)Ú
access_logé   )ÚUser)Úprometheus_log_method>   ÚstateÚkeyÚcodeZxsrfÚauthÚtoken)ÚuriÚreturnc           
      C   sŠ   t | ƒ}|jr†|j d¡}d}t|ƒD ]@\}}| d¡\}}}tD ]"}	|	|v rB|› |› d�||< d}qBq&|r†|jd |¡d�}t|ƒS | S )zscrub auth info from uriú&Fú=z[secret]T)Úquery)	r   r   ÚsplitÚ	enumerateÚ	partitionÚ_SCRUB_PARAM_KEYSÚ_replaceÚjoinr   )
r   ÚparsedÚpartsÚchangedÚiÚsr	   ÚsepÚvalueZ	substring© r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/log.pyÚ
_scrub_uri   s    r!   c                 C   sx  |   ¡ }| j}z
| j}W n ty.   t}Y n0 |dk s@|dkrH|j}n&|dk rX|j}n|dk rh|j}n|j}d| j 	¡  }||j
|jt|jƒ|dœ}z
| j}W n ty¶   d}Y n0 |rÐt|tƒrÌ|jqÒdnd	}||d
< d}	|dk�rt|j dd¡ƒ|d< |	d }	|dk�rX|dk�rXi }
dD ] }||jv �r$|j| |
|< �q$|tj|
dd�ƒ ||	jf i |¤Žƒ t| ƒ dS )a  log a bit more information about each request than tornado's default

    - move static file get success to debug-level (reduces noise)
    - get proxied IP instead of proxy IP
    - log referer for redirect and failed requests
    - log user-agent for failed requests
    i,  i0  i�  iô  g     @�@)ÚstatusÚmethodÚipr   Úrequest_timeNÚunknownÚ Úusernamez>{status} {method} {uri} ({username}@{ip}) {request_time:.2f}msÚRefererÚNoneZrefererz referer={referer}iö  )ÚHostÚAcceptr)   z
User-Agenté   )Úindent)Z
get_statusÚrequestÚlogÚAttributeErrorr   ÚdebugÚinfoÚwarningÚerrorr%   r#   Z	remote_ipr!   r   Zcurrent_userÚ	ExceptionÚ
isinstancer   r(   ÚheadersÚgetÚjsonÚdumpsÚformatr   )Úhandlerr"   r/   ÚloggerZ
log_methodr%   ÚnsÚuserr(   Úmsgr8   Úheaderr   r   r    Úlog_request+   sL    

û



rC   )Ú__doc__r:   Úurllib.parser   r   Ztornado.logr   r   r   Zprometheus.log_functionsr   r   Ústrr!   rC   r   r   r   r    Ú<module>   s   