
Ke                 @   s   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 d  d l	 m
 Z
 m Z m Z Gd d   d e j  Z Gd d   d e j  Z e d	 d
 d    Z e e d	 d d    Z d d   Z d S)    N)OrderedDict)AnyDictList)defaultssystemutilc               @   s.   e  Z d  Z d Z e j d d d    Z d S)RedactionFilterz,A logging filter to redact confidential info)recordc             C   s   t  j t | j   | _ d S)NT)r   Zredact_sensitive_logsstrmsg)selfr
    r   ./usr/lib/python3/dist-packages/uaclient/log.pyfilter   s    zRedactionFilter.filterN)__name__
__module____qualname____doc__logging	LogRecordr   r   r   r   r   r	   
   s   r	   c               @   sC   e  Z d  Z d Z d Z d Z d Z e j e	 d
 d d    Z
 d S)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message)r
   returnc             C   s  | j    | _ |  j |  | _ i  } | j rF |  j | j  | d <| j d  rl | j rl | j | d <| j r |  j	 | j  | d <| j
 j d  } | r t | t  r | j |  t   } x- |  j D]" } | j
 j |  } | | | <q W| | d <t j t | j     S)Nexc_info
stack_infoextra)Z
getMessager   Z
formatTimer   r   ZformatExceptiongetZexc_textr    ZformatStack__dict__
isinstancedictupdater   required_fieldsjsondumpslistvalues)r   r
   Zextra_message_dictr!   Zlocal_log_recordZfieldvaluer   r   r   format"   s(    			
zJsonArrayFormatter.formatN)zasctimer   znamer   zlinenozmessage)r   r   r   r   Zdefault_time_formatZdefault_msec_formatr'   r   r   r   r-   r   r   r   r   r      s        r   )r   c               C   s   t  j   d S)z/Gets the correct user log_file storage locationz/ubuntu-pro.log)r   Zget_user_cache_dirr   r   r   r   get_user_log_file@   s    r.   c              C   sb   t  j d  }  g  } xF |  D]> } d | d t j d } t  j j |  r | j |  q W| 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/home/z/.cache/z/ubuntu-pro.log)oslistdirr   ZUSER_CACHE_SUBDIRpathisfileappend)Zuser_directoriesZ	log_filesZuser_directoryZ	user_pathr   r   r   get_all_user_log_filesE   s    r4   c             C   sW   | j  |   | j t    t j   } | j t    | j  |   | j |  d  S)N)ZsetLevelZ	addFilterr	   r   ZStreamHandlerZsetFormatterr   Z
addHandler)Z	log_levelZloggerZconsole_handlerr   r   r   setup_journald_loggingY   s    r5   )r(   r   r/   collectionsr   Ztypingr   r   r   Zuaclientr   r   r   ZFilterr	   Z	Formatterr   r   r.   r4   r5   r   r   r   r   <module>   s   .