
    5fT                     t   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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 d dlmZ d Zdad Zd Zd	 Zd
 Z G d de      Z ej:                  d      Zd Z G d dej@                        Z! G d dej@                        Z" G d dej@                        Z#y)    N)tzcontext)	jsonutils)encodeutilsc                     t        | dd       r| j                         S t        | dd       r(t        j                  ddd       | j	                         S t        | t              r| S i S )Nget_logging_valuesto_dictzThe RequestContext.get_logging_values() method should be defined for logging context specific information.  The to_dict() method is deprecated for oslo.log use.z3.8.0z5.0.0)versionremoval_version)getattrr	   debtcollector	deprecater
   
isinstancedictr   s    5/usr/lib/python3/dist-packages/oslo_log/formatters.py_dictify_contextr   !   sj    w,d3))++	)T	*  *1'		K
    
GT	"I    c                     | a y N)_CONF)confs    r   _store_global_confr   8   s    Er   c                     | j                   j                  dt        j                               }|r0t	        |      }|j                         D ]  \  }}t        | ||        |S )zGiven a log record, update it with context information.

    The request context, if there is one, will either be passed with the
    incoming record or in the global thread-local store.
    r   )__dict__getcontext_utilsget_currentr   itemssetattr)recordr   dkvs        r   _update_record_with_contextr%   =   sd     oo!!!!#G W% GGI 	"DAqFAq!	" Nr   c                     t        | t              r| S t        | t              st        |       S t        j                  | dd      S )zBDo our best to turn the input argument into a unicode object.
    zutf-8xmlcharrefreplace)incomingerrors)r   strbytesr   safe_decode)msgs    r   _ensure_unicoder.   Q   sB     #s
c5!3x"""$ $r   c                    d}| j                   t        j                  k  ry| j                  r| j                  }n>t	        j                         }|d   sd}n"|d   t
        t        t        t        t        fv rd}|rR	 t        j                  |d   |d         d   j                         }| j                  s|j                  dd      d   }~|S |S # t
        $ r}dt        |      z  }Y d}~~|S d}~ww xY w# ~w xY w)a  Return the error summary

    If there is no active exception, return the default.

    If the record is being logged below the warning level, return an
    empty string.

    If there is an active exception, format it and return the
    resulting string.

     r   N   
z<exception with %s>)levelnologgingWARNINGexc_infosys	TypeError
ValueErrorKeyErrorAttributeErrorImportError	tracebackformat_exception_onlyrstripsplitr*   )r!   error_summaryr6   type_errs       r   _get_error_summaryrC   ^   s    M~~' ??
 <<> {Ha[Y
%~{D D H 	 &;;    ?? - 3 3D! <Q ? =  	B1CMAM
 	B s+   ;AC 	C1C,%C4 ,C11C4 4C7c                       e Zd Zd Zy)_ReplaceFalseValuec                 8    t         j                  | |d       xs dS )N-)r   r   )selfkeys     r   __getitem__z_ReplaceFalseValue.__getitem__   s    xxc4(/C/r   N)__name__
__module____qualname__rJ    r   r   rE   rE      s    0r   rE   z(%+)\((\w+)\)c                     t        j                  t        j                  t              }t        j
                  | |      S )N)fallback)default)	functoolspartialr   to_primitivereprdumps)objconverts     r   _json_dumps_with_fallbackrY      s-     	 6 6FG??300r   c                   "    e Zd ZddZddZd Zy)JSONFormatterNc                     || _         	 t        j                         | _        y # t        j                  $ r
 d | _        Y y w xY wr   )datefmtsocketgethostnamehostnameerror)rH   fmtr]   styles       r   __init__zJSONFormatter.__init__   s:     	!"..0DM|| 	! DM	!s   # A A c                 *   	 t        j                  | }|rQ|D cg c]+  }t	        d |j                         j                               - }}t        t        j                  |       }|S # t        $ r}t        |      }d|z  g}Y d }~td }~ww xY wc c}w )N"<Unprintable exception due to %s>
c                     | S r   rN   )xs    r   <lambda>z/JSONFormatter.formatException.<locals>.<lambda>   s    ! r   )
r=   format_exceptionr8   r*   filterr?   
splitlineslist	itertoolschain)rH   eistrip_newlineslines
type_errorr-   lines          r   formatExceptionzJSONFormatter.formatException   s    	B..3E
  9>?04 ((*, ?E ? %01E  	Bj/C:S@AE	B
?s   A) 0B)	B2BBc                    |j                   }t        |t              rmt        j	                  |j
                        }|D cg c]  }t        |d         dk(  s|d    }}|r(|j                         D ci c]  \  }}||v s|| }}}i d|j                         d| j                  || j                        d|j                  d|j
                  d|d|j                  d	|j                  d
|j                  d|j                  d|j                   d|j"                  d|j$                  d|j&                  d|j(                  d|j*                  d|j,                  d|j.                  |j0                  |j2                  d | j4                  t7        |      d}t9        |      }t;        |d      r|j<                  j?                         }	ni }	tA        |dg       D ]  }
|
|	vstA        ||
      |	|
<    d|	v r|	d   rtC        |	d         |d<   n|rtC        |      |d<   ni |d<   |	jE                  dd        |	|d<   |jF                  r| jI                  |jF                        |d<   tK        |      S c c}w c c}}w )Nr   r1   messageasctimenamer-   args	levelnamer3   pathnamefilenamemodulelinenofuncnamecreatedmsecsrelative_createdthreadthread_name)process_nameprocessr=   r`   rA   extra
extra_keysr   r=   )&rz   r   r   _MSG_KEY_REGEXfindallr-   lenr   
getMessage
formatTimer]   ry   r{   r3   r|   r}   r~   r   funcNamer   r   relativeCreatedr   
threadNameprocessNamer   r`   rC   r%   hasattrr   copyr   r   popr6   ru   rY   )rH   r!   rz   msg_keysmr#   r$   rw   r   r   rI   s              r   formatzJSONFormatter.format   s   {{dD!%--fjj9H '/A#ad)q.!AHA )-GAh1GG@9f//1 @doofdllC@6;;@ &**@ 4	@
  0 0@ fnn@ v@ v@ V]]@ V]]@ v@ fnn@ FLL@ &v'='=@ V]]@  !&"3"3!@" $*#5#5$nn $#}}$6v$>+@2 .f567#LL%%'EE6<4 	2C%$VS1c
	2 %	"2!1%	2B!CGI!1'!:GI!#GI		)T" ??#'#7#7#HGK (11o B Hs    I9I95I>I>)NN%T)rK   rL   rM   rd   ru   r   rN   r   r   r[   r[      s    !>2r   r[   c                   &    e Zd ZdZddZddZd Zy)FluentFormatterzA formatter for fluentd.

    format() returns dict, not string.
    It expects to be used by fluent.handler.FluentHandler.
    (included in fluent-logger-python)

    .. versionadded:: 3.17
    Nc                 H   || _         	 t        j                         | _        dj                  t        j                        | _        	 dd l	}|j                  j                  d      }|| _        y # t        j                  $ r
 d | _        Y hw xY w# t        $ r
 d | _        Y y w xY w)N r   ry   )r]   r^   r_   r`   ra   joinr7   argvcmdlineuwsgioptr   
uwsgi_name	Exception)rH   rb   r]   rc   r   svc_names         r   rd   zFluentFormatter.__init__  s     	!"..0DM xx)	#yy}}V,H&DO || 	! DM	!  	#"DO	#s#   A. &B .B
BB! B!c                     	 t        j                  | }|rt	        j
                  d |g       }|S # t        $ r}t        |      }d|z  g}Y d }~;d }~ww xY w)Nrf   c                 D    | |j                         j                         z   S r   )r?   rl   )art   s     r   ri   z1FluentFormatter.formatException.<locals>.<lambda>5  s    +,t{{}/G/G/I+I r   )r=   rj   r8   r*   rR   reduce)rH   r6   rq   rr   rs   r-   s         r   ru   zFluentFormatter.formatException-  si    	B..9E
 $$ &J%*B0E   	Bj/C:S@AE	Bs   0 	AAAc                    |j                         | j                  || j                        |j                  |j                  |j
                  |j                  |j                  |j                  |j                  | j                  | j                  d t        |      d}t        |      }t        |d      r|j                  j!                         }ni }t#        |dg       D ]  }||vst#        ||      ||<    d|v r|d   rt%        |d         |d<   n|rt%        |      |d<   ni |d<   |j'                  dd        t(        t*        t,        t/        d       t0        t2        t4        f}|j7                         D ]   \  }}t9        ||      rt;        |      ||<   " ||d<   |j<                  r| j?                  |j<                        |d<   | j@                  r| j@                  |d<   |S )N)rw   timery   levelr}   r   r~   r   r   r   r`   r=   rA   r   r   r   r=   r   )!r   r   r]   ry   r{   r}   r   r~   r   r   r   r`   rC   r%   r   r   r   r   r   r   r*   intbooltypefloatrm   r   r   r   rY   r6   ru   r   )rH   r!   rw   r   r   rI   primitive_typesvalues           r   r   zFluentFormatter.format:  s   $//1??64<<@!;;",,%#]]#]]%#)#5#5"ll#}} $$6v$>@  .f567#LL%%'EE6<4 	2C%$VS1c
	2 %	"2!1%	2B!CGI!1'!:GI!#GI		)T"T4:udDI++- 	>JCe_56u=c
	> !??#'#7#7#HGK ??$(OOGL!r   )NNz%sr   )rK   rL   rM   __doc__rd   ru   r   rN   r   r   r   r     s    #"2r   r   c                   *    e Zd ZdZd Zd ZddZd Zy)ContextFormattera  A context.RequestContext aware formatter configured through flags.

    The flags used to set format strings are: logging_context_format_string
    and logging_default_format_string.  You can also specify
    logging_debug_format_suffix to append extra formatting if the log level is
    debug.

    The standard variables available to the formatter are listed at:
    http://docs.python.org/library/logging.html#formatter

    In addition to the standard variables, one custom variable is
    available to both formatting string: `isotime` produces a
    timestamp in ISO8601 format, suitable for producing
    RFC5424-compliant log messages.

    Furthermore, logging_context_format_string has access to all of
    the data in a dict representation of the context.
    c                     |j                  dd      | _        |j                  dd      | _        |j                  dt              | _        t        j                  j                  | g|i | y)a   Initialize ContextFormatter instance

        Takes additional keyword arguments which can be used in the message
        format string.

        :keyword project: project name
        :type project: string
        :keyword version: project version
        :type version: string

        projectunknownr   configN)r   r   r   r   r   r4   	Formatterrd   )rH   rz   kwargss      r   rd   zContextFormatter.__init__  sX     zz)Y7zz)Y7JJx/	""49$9&9r   c                    | j                   |_         | j                  |_        d}t        |dd      }t        |dd      }t        |      }|r	 | j                  j
                  |z  }n|r| j                  j                  d|iz  }n|rt        |dd      }t        |dd      }t        |dd      }|r| j                  j
                  d|iz  }n;|r| j                  j                  d|iz  }n|r| j                  j                  d|iz  }||_        dD ]   }||j                  vsd|j                  |<   " |r0| j                  j                  t        t        |            z  |_        |j                  j                  d      r| j                  j                  }n| j                  j                   }|j"                  r!| j%                  |j"                  |      |_        t)        |      |_        d	|v r|j*                  xs d
|_        n|j*                  r|dz  }|j,                  t.        j0                  k(  r2| j                  j2                  r|d| j                  j2                  z   z  }| j5                  |       t/        j6                  |      | _        | j8                  j:                  | _        	 t.        j<                  j?                  | |      S # t        $ r |}Y w xY w# t        $ r[}	dj?                  |j@                  |	      jC                  dd      |_         t.        j<                  j?                  | |      cY d}	~	S d}	~	ww xY w)z;Uses contextstring if request_id is set, otherwise default.r0   instanceNinstance_uuiduuidresource_uuid)r   coloruser_identityresource	user_nameproject_nameglobal_request_id
request_idz%(error_summary)srG   z: %(error_summary)sr   z+Error formatting log line msg={!r} err={!r}r   *)"r   r   r   r%   r   instance_formatr8   instance_uuid_formatr   r   logging_user_identity_formatrE   r   r   r   logging_context_format_stringlogging_default_format_stringr6   ru   exc_textrC   rA   r3   r4   DEBUGlogging_debug_format_suffix_compute_iso_timePercentStyle_style_fmtr   r   r-   replace)
rH   r!   instance_extrar   r   r   r   rI   rb   errs
             r   r   zContextFormatter.format  s    
 6:t4>-f5*"&))";";$,#- "ii<<!' 78N w
D9H#G_dCM $G_dCM"&))";";%+X$6#7"&))"@"@%+]$;#<"&))"@"@%+]$;#< )
F 	*C&//)')$	* 		66"#3G#<=>  
 ??|,))99C))99C ??"226??FKFO1&9#% $*#7#7#>3F !! ((CNNgmm+		553>>>>Cv&**3/KK$$		:$$++D&99[  *!)*\  	: GMM

C!!(c!2 J$$++D&99	:s1   K +K KK	M&AL<6M<MNc                 (   |s!	 t         j                  j                  | |      S t        j                         }	 t        j                  |d   |d   |d   d|       |j                         j                  d      }|j                          | j                  j                  j                  d      dk7  r!| j!                  || j"                        |_        | j'                  |       g }|D ]<  }| j                  j                  |j(                  z  }	|	|}
|j+                  |
       > dj-                  |      S # t        $ r}t	        |      }d|z  cY d}~S d}~ww xY w# t        $ r*}t	        |      }|j                  d|z         Y d}~1d}~ww xY w)	z;Format exception output with CONF.logging_exception_prefix.rf   Nr   r1      r2   z
%(asctime))r4   r   ru   r8   r*   ioStringIOr=   print_exceptionwritegetvaluer@   closer   logging_exception_prefixfindr   r]   rx   r   r   appendr   )rH   r6   r!   rs   r-   stringbufferrr   formatted_linesrt   plfls              r   ru   z ContextFormatter.formatException  st   C((88xHH {{}	L%%hqk8A;&*L: %%'--d399--22<@BF!__VT\\BFNv& 	'D33fooEBt$B""2&	' yy))7  C*o<sBBC  	Lj/CDsJKK	Ls4   D8 "E 8	EEEE	F'FFc                 V   t        j                         }t        j                  j                  |j                        j                  |      j                         |_        |j                  t        |j                        k(  r'|j                  d d d|j                  dd  |_        y y )N)tzinfoiz.000000)	r   tzlocaldatetimefromtimestampr   r   	isoformatisotimer   )rH   r!   localtzs      r   r   z"ContextFormatter._compute_iso_time  s    **,!**88NN#G7G3IIK 	>>S00 /5nnSb.A.4nnRS.ACFN 1r   r   )rK   rL   rM   r   rd   r   ru   r   rN   r   r   r   r   o  s    &:&d:L *DCr   r   )$r   r   rR   r   rn   r4   logging.configlogging.handlersrer^   r7   r=   dateutilr   oslo_contextr   r   oslo_serializationr   
oslo_utilsr   r   r   r   r%   r.   rC   r   rE   compiler   rY   r   r[   r   r   rN   r   r   <module>r      s       	     	  
   1 ( "( 	
(
$EP0 0
 ,-1W2G%% W2tZg'' Zz|Cw(( |Cr   