
    s
e                        U d Z g d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
mZ dZeed<   ej        ej        ej        e         eej        ej                 f         ej        d         f         Zej        eeeedf         Z G d d	ej                  Zee
fd
ededej        e         fdZefd
eddfdZdedej        ej        eej        f                  fdZ dS )zLogger utilities.)	log_files
clean_logsload_logJSONFormatter    N)LOG_FILENAME
LOG_FOLDERz%Y-%m-%d %H:%M:%SLOG_TIME_FORMAT)NNNc            	            e Zd ZdZddefdej        ej        eef                  dej        ej        eef                  dej        e         ddf fdZ	de
fdZd	ej        defd
Z xZS )r   z9
    Simple logs formatter using JSON serialization.
    Nfieldsalways_extradatefmtreturnc                     t                                          d|d           |pi | _        d| j                                        v | _        |pi | _        dS )a  
        :param fields: A dictionary of fields to use in the log.
            The keys in the dictionary are keys that will be used in the
            final log form, and its values are the names of the attributes
            from the log record to use as final log values. Defaults to
            None, which is interpreted as an empty dict.
        :param always_extra: A dictionary of additional static
            values written to the final log. Defaults to None, which is
            interpreted as an empty dict.
        :param datefmt: strftime date format. For more details
            check logs.Formatter documentation. Defaults to None.
        N%)fmtr   styleasctime)super__init__r   values
_uses_timer   )selfr   r   r   	__class__s       Clib/python3.11/site-packages/navigator_updater/utils/logs/common.pyr   zJSONFormatter.__init__'   s\    $ 	T7#>>>-3\r )T[-?-?-A-A A:F:L"    c                     | j         S )z
        Check if the format uses the creation time of the record. For more
        information about the method see logs.Formatter.
        )r   )r   s    r   usesTimezJSONFormatter.usesTime>   s    
 r   recordc                 j   | j                                         }|                                |_        |                                 r |                     || j                  |_        |j        r&|j	        s| 
                    |j                  |_	        |j        r|                     |j                  |_        | j                                        D ]M\  }}|j                            |d          }|s#|dk    rt#          |j        pg           r|j	        nd}|||<   Nt%          j        |          S )a?  
        Build a JSON serializable dict starting from `self.always_extra`,
        adding the data from the LogRecord specified in `self.fields`, and
        finally adding the record specific extra data.

        :param record: log record to be converted to string
        :returns: JSON serialized log record
        Nexc_text)r   copy
getMessagemessager   
formatTimer   r   exc_infor    formatException
stack_infoformatStackr   items__dict__getanyujsondumps)r   r   datakeyfieldvalues         r   formatzJSONFormatter.formatE   s.    8<7H7M7M7O7O**,,==?? 	C!__VT\BBFN? 	D6? 	D"226?CCFO 	D $ 0 01B C CF +++-- 		 		JC(.(;(;E4(H(HE 
""+.v/D"+E+EO4DII{4   r   )__name__
__module____qualname____doc__r	   typingOptionalDictstrr   boolr   logging	LogRecordr3   __classcell__)r   s   @r   r   r   "   s          >BCG,;	M MOFKS$9:M !/&+c3h*?@M _S)	M
 
M M M M M M.$    "!W. "!3 "! "! "! "! "! "! "! "!r   r   
log_folderlog_filenamer   c                 n   g }t           j                            |           s|S t          t          j        |                     D ]o}t           j                            | |          }t           j                            |          r.||v r*|                    d          s|                    |           p|S )z
    Return all available log files located inside the logs folder.

    Files starting with a `.` are ignored as well as files not including the
    `log_filename` as part of the name.
    .)	ospathisdirsortedlistdirjoinisfile
startswithappend)r@   rA   pathslog_filelog_file_paths        r   r   r   j   s     !E7==$$  2:j1122 ( (W\\*h??7>>-(( 	(lh.F.FQYQdQdehQiQi.FLL'''Lr   c                 ,   t           j                                         t          j        d          z
  }t           j         j        }t	          |           D ]}t          |          }g }|D ]i}	  ||d         t                    |k     rn# t          t          t          f$ r Y nw xY w|
                    t          j        |d          dz              jt          |dd	          5 }|                    |           d
d
d
           n# 1 swxY w Y   d
S )z%Remove logs in old plain text format.   )daystimeF)ensure_ascii
wutf-8encodingN)datetimenow	timedeltastrptimer   r   r	   
ValueError	TypeErrorKeyErrorrL   r-   r.   open
writelines)r@   week_agoto_datetimerE   lines	new_lineslinestreams           r   r   r      sr   "*"3"7"7"9"9H<NTU<V<V<V"VHBJBSB\K *%% ) ) ')	 	K 	KD;tF|_==HH I	84   U[EBBBTIJJJJ$g... 	)&i(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)) )s$   -B

B$#B$&DD	D	rO   c                    t           j                            |           sg S g }t          | dd          5 }|D ]:}	 |                    t          j        |                     +# t          $ r Y 7w xY w	 ddd           n# 1 swxY w Y   |S )z'Load log file and return list of items.rrW   rX   N)rD   rE   rJ   ra   rL   r-   loadsr^   )rO   
json_linesrh   rg   s       r   r   r      s    7>>-(( 	?AJ	mS7	3	3	3 v 	 	D!!%+d"3"34444   	               s4   B'A$#B$
A1.B0A11BBB)!r7   __all__rZ   logging.handlersr=   rD   typesr8   r-   navigator_updater.configr   r   r	   r;   __annotations__UnionTupleTypeBaseExceptionr9   TracebackType_SysExcInfoTypeintfloat_LogRecordAttrType	Formatterr   Listr   r   MappingAnyr    r   r   <module>r      s     
B
B
B      				    = = = = = = = =* * * *,
L]+]FOEL_<``a
L!"#
 \#uc?D"HI E! E! E! E! E!G% E! E! E!P !+  # # QWQ\]`Qa    * ", ) )3 ) ) ) ) )0C FKsFJ0O$P      r   