>f4ddlZddlZddlZddlZddlmZddlmZmZm Z m Z ddl m Z m Z mZmZddlmZGddej$ZGdd ej$ZGd d ej*Zd efd Zd efdZd e efdZdZde eefdefdZy)N) OrderedDict)AnyDictListUnion)defaultssecret_managersystemutil)UAConfigc0eZdZdZdej fdZy)RegexRedactionFilter,A logging filter to redact confidential inforecordc^tjt|j|_yNT)r redact_sensitive_logsstrmsgselfrs ./usr/lib/python3/dist-packages/uaclient/log.pyfilterzRegexRedactionFilter.filters//FJJ@ N__name__ __module__ __qualname____doc__logging LogRecordrrrrr 6W..rrc0eZdZdZdej fdZy)KnownSecretRedactionFilterrrcrtjjt|j|_yr)r secretsredact_secretsrrrs rrz!KnownSecretRedactionFilter.filters%#++::3vzz?K rNrr"rrr%r%r#rr%c@eZdZdZdZdZdZdejde fdZ y) JsonArrayFormatterzYJson Array Formatter for our logging mechanism Custom made for Pro logging needs z%Y-%m-%dT%H:%M:%Sz%s.%03d)asctime levelnamenamefuncNamelinenomessagerreturnc|j|_|j||_i}|jr|j |j|d<|j ds|jr|j|d<|jr|j|j|d<|jj d}|r!t|tr|j|t}|jD]"}|jj |}|||<$||d<t!j"t%|j'S)Nexc_info stack_infoextra) getMessager0 formatTimer+r3formatExceptiongetexc_textr4 formatStack__dict__ isinstancedictupdaterrequired_fieldsjsondumpslistvalues)rrextra_message_dictr5local_log_recordfieldvalues rformatzJsonArrayFormatter.format,s2**,0 ??-1-A-A. z *"%%j1foo-3__ z *   /3/?/?!!0 | ,##G, Zt,  % %e ,'=)) ,EOO''.E&+ U # ,%7!zz$/6689::rN) rrrrdefault_time_formatdefault_msec_formatr@r r!rrIr"rrr*r*s5.#O;W..;3;rr*r1cftjrtjSt S)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_filer"rrget_user_or_root_log_file_pathrPJs'  !!#z""" ""rcftjjtjdS)z/Gets the correct user log_file storage locationubuntu-pro.log)ospathjoinr get_user_cache_dirr"rrrOrOUs! 77<<1135E FFrctjd}g}|D]d}tjjd|dtj d}tjj |sT|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)rSlistdirrTrUrUSER_CACHE_SUBDIRisfileappend)user_directories log_filesuser_directory user_paths rget_all_user_log_filesr`Zsv zz'*I* (GGLL     & &    77>>) $   Y ' ( rctjd}|jtjtj}|j t |jtj|jt|jt|j|y)N ubuntupro) r getLoggersetLevelINFO StreamHandler setFormatterr* addFilterrr% addHandler)loggerconsole_handlers rsetup_journald_loggingrlns   { +F OOGLL!++-O  !3!56W\\*2458:; o&r log_levelrNct|tr|j}tjs t }t jd}|j|g|_ tj|}|js/|jjdd|jdt 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. rbT)parentsexist_oki)modeN)r=rupperr rMrOr rcrdhandlerspathlibPathexistsparentmkdirtouch FileHandlerrgr*rhrr%ri)rmrNrj log_file_path file_handlers rsetup_cli_loggingr}ys )S!OO%   % % '$&   { +F OOIFOLL*M    !""4$"?'&&x0L023)$/12578 l#r)rAr rSrt collectionsrtypingrrrruaclientrr r r uaclient.configr Filterrr% Formatterr*rrPrOr`rlintr}r"rrrs  #));;$7>>+;**+;\##G3G S ('$sCx$C$r