
    g                     :   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
 d dlmZmZmZmZ d dlmZ  G d dej$                        Z G d d	ej$                        Z G d
 dej*                        ZdefdZdefdZde	e   fdZd Zde
eef   defdZd Zy)    N)OrderedDict)AnyDictListUnion)defaultssecret_managersystemutil)UAConfigc                   0    e Zd ZdZdej
                  fdZy)RegexRedactionFilter,A logging filter to redact confidential inforecordc                 ^    t        j                  t        |j                              |_        yNT)r   redact_sensitive_logsstrmsgselfr   s     ./usr/lib/python3/dist-packages/uaclient/log.pyfilterzRegexRedactionFilter.filter   s    //FJJ@
    N__name__
__module____qualname____doc__logging	LogRecordr    r   r   r   r          6W.. r   r   c                   0    e Zd ZdZdej
                  fdZy)KnownSecretRedactionFilterr   r   c                 r    t         j                  j                  t        |j                              |_        yr   )r	   secretsredact_secretsr   r   r   s     r   r   z!KnownSecretRedactionFilter.filter   s%    #++::3vzz?K
r   Nr   r"   r   r   r%   r%      r#   r   r%   c                   @    e Zd ZdZdZdZdZdej                  de	fdZ
y)	JsonArrayFormatterzYJson Array Formatter for our logging mechanism
    Custom made for Pro logging needs
    z%Y-%m-%dT%H:%M:%Sz%s.%03d)asctime	levelnamenamefuncNamelinenomessager   returnc                    |j                         |_        | j                  |      |_        i }|j                  r| j                  |j                        |d<   |j                  d      s|j                  r|j                  |d<   |j                  r| j                  |j                        |d<   |j                  j                  d      }|r!t        |t              r|j                  |       t               }| j                  D ]"  }|j                  j                  |      }|||<   $ ||d<   t!        j"                  t%        |j'                                     S )Nexc_info
stack_infoextra)
getMessager0   
formatTimer+   r3   formatExceptiongetexc_textr4   formatStack__dict__
isinstancedictupdater   required_fieldsjsondumpslistvalues)r   r   extra_message_dictr5   local_log_recordfieldvalues          r   formatzJsonArrayFormatter.format,   s2   **,0??-1-A-A.z* "%%j1foo-3__z*/3/?/?!!0|, ##G,Zt,%%e, '=)) 	,EOO''.E&+U#	, %7!zz$/6689::r   N)r   r   r   r   default_time_formatdefault_msec_formatr@   r    r!   r   rI   r"   r   r   r*   r*      s5     .#O;W.. ;3 ;r   r*   r1   c                  f    t        j                         rt               j                  S t	               S )z\
    Gets the correct log_file path,
    adjusting for whether the user is root or not.
    )r   we_are_currently_rootr   log_fileget_user_log_filer"   r   r   get_user_or_root_log_file_pathrP   J   s'    
 !!#z""" ""r   c                  f    t         j                  j                  t        j                         d      S )z/Gets the correct user log_file storage locationubuntu-pro.log)ospathjoinr
   get_user_cache_dirr"   r   r   rO   rO   U   s!    77<<1135EFFr   c                     t        j                  d      } g }| D ]d  }t         j                  j                  d|dt        j
                  d      }t         j                  j                  |      sT|j                  |       f |S )z|Gets all the log files for the users in the system

    Returns a list of all user log files in their home directories.
    z/homez.cacherR   )rS   listdirrT   rU   r   USER_CACHE_SUBDIRisfileappend)user_directories	log_filesuser_directory	user_paths       r   get_all_user_log_filesr`   Z   sv    
 zz'*I* 	(GGLL&&
	 77>>)$Y'	( r   c                     t        j                  d      } | j                  t         j                         t        j                         }|j                  t                      |j                  t         j                         |j                  t                      |j                  t                      | j                  |       y )N	ubuntupro)r    	getLoggersetLevelINFOStreamHandlersetFormatterr*   	addFilterr   r%   
addHandler)loggerconsole_handlers     r   setup_journald_loggingrl   n   s    {+F
OOGLL!++-O  !3!56W\\*2458:;
o&r   	log_levelrN   c                    t        | t              r| j                         } t        j                         s
t               }t        j                  d      }|j                  |        g |_	        t        j                  |      }|j                         s/|j                  j                  dd       |j                  d       t        j                   |      }|j#                  t%                      |j                  |        |j'                  t)                      |j'                  t+                      |j-                  |       y)zoSetup logging to log_file

    If run as non-root then log_file is replaced with a user-specific log file.
    rb   T)parentsexist_oki  )modeN)r=   r   upperr   rM   rO   r    rc   rd   handlerspathlibPathexistsparentmkdirtouchFileHandlerrg   r*   rh   r   r%   ri   )rm   rN   rj   log_file_pathfile_handlers        r   setup_cli_loggingr}   y   s     )S!OO%	 %%'$&{+F
OOI FO LL*M!""4$"?'&&x0L023)$/12578
l#r   c                  
    d| iS )z
    A helper for passing extra fields to log statements.
    Usage:
    LOG.info("message string", extra=log.extra(field=something_relevant))
    r5   r"   )kwargss    r   r5   r5      s     Vr   )rA   r    rS   rt   collectionsr   typingr   r   r   r   uaclientr   r	   r
   r   uaclient.configr   Filterr   r%   	Formatterr*   r   rP   rO   r`   rl   intr}   r5   r"   r   r   <module>r      s      	  # ) ) ; ; $7>>  +;** +;\# #G3 G
S	 ('$sCx $C $Br   