o >f@sddlZddlZddlZddlZddlmZddlmZmZm Z m Z ddl m Z m Z mZmZddlmZGdddejZGdd d ejZGd d d ejZd efd dZd efddZd e efddZddZde eefdefddZdS)N) OrderedDict)AnyDictListUnion)defaultssecret_managersystemutil)UAConfigc@ eZdZdZdejfddZdS)RegexRedactionFilter,A logging filter to redact confidential inforecordcCstt|j|_dSNT)r redact_sensitive_logsstrmsgselfrr./usr/lib/python3/dist-packages/uaclient/log.pyfilterszRegexRedactionFilter.filterN__name__ __module__ __qualname____doc__logging LogRecordrrrrrr r c@r )KnownSecretRedactionFilterrrcCstjt|j|_dSr)rsecretsredact_secretsrrrrrrrsz!KnownSecretRedactionFilter.filterNrrrrrr!r r!c@s0eZdZdZdZdZdZdejde fddZ 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 levelnamenamefuncNamelinenomessagerreturncCs||_|||_i}|jr||j|d<|ds%|jr%|j|d<|jr0| |j|d<|j d}|rBt |t rB| |t}|jD] }|j |}|||<qH||d<tt|S)Nexc_info stack_infoextra) getMessager* formatTimer%r,formatExceptiongetexc_textr- formatStack__dict__ isinstancedictupdaterrequired_fieldsjsondumpslistvalues)rrextra_message_dictr.local_log_recordfieldvaluerrrformat,s,        zJsonArrayFormatter.formatN) rrrrdefault_time_formatdefault_msec_formatr9rrrrBrrrrr$s  r$r+cCstrtjStS)z\ Gets the correct log_file path, adjusting for whether the user is root or not. )r we_are_currently_rootr log_fileget_user_log_filerrrrget_user_or_root_log_file_pathJsrHcCstjtdS)z/Gets the correct user log_file storage locationubuntu-pro.log)ospathjoinr get_user_cache_dirrrrrrGUsrGcCsHtd}g}|D]}tjd|dtjd}tj|r!||q |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.cacherI)rJlistdirrKrLrUSER_CACHE_SUBDIRisfileappend)user_directories log_filesuser_directory user_pathrrrget_all_user_log_filesZs   rVcCs\td}|tjt}|t|tj|t|t | |dS)N ubuntupro) r getLoggersetLevelINFO StreamHandler setFormatterr$ addFilterr r! addHandler)loggerconsole_handlerrrrsetup_journald_loggingns      ra log_levelrFcCst|tr |}tst}td}||g|_ t |}| s4|j jddd|jddt|}|t|||t|t||dS)zoSetup logging to log_file If run as non-root then log_file is replaced with a user-specific log file. rWT)parentsexist_oki)modeN)r6rupperr rErGrrXrYhandlerspathlibPathexistsparentmkdirtouch FileHandlerr\r$r]r r!r^)rbrFr_ log_file_path file_handlerrrrsetup_cli_loggingys"          rq)r:rrJrh collectionsrtypingrrrruaclientrrr r uaclient.configr Filterr r! Formatterr$rrHrGrVraintrqrrrrs   .