a €šçdŽã@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z dZ ejZdad+dd„Zdd „Zd d „Zd d „Zdd„Zdd„Zdd„Zdd„Ze dej¡Zdd„ZGdd„deƒZGdd„deeƒZ Gdd„de!eƒZ"Gdd „d e#eƒZ$Gd!d"„d"eƒZ%Gd#d$„d$e%ƒZ&e&Z'd%d&„Z(e dfd'd(„Z)d)d*„Z*dS),a Configuration functions for the logging package for Python. The core package is based on PEP 282 and comments thereto in comp.lang.python, and influenced by Apache's log4j system. Copyright (C) 2001-2019 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! éN)ÚThreadingTCPServerÚStreamRequestHandleriF#TcCsŒddl}t||jƒr|}n*| |¡}t|dƒr:| |¡n | |¡t|ƒ}t  ¡z(t ƒt ||ƒ}t |||ƒWt  ¡n t  ¡0dS)aD Read the logging configuration from a ConfigParser-format file. This can be called several times from an application, allowing an end user the ability to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen configuration). rNÚreadline)Ú configparserÚ isinstanceÚRawConfigParserÚ ConfigParserÚhasattrÚ read_fileÚreadÚ_create_formattersÚloggingÚ _acquireLockÚ_clearExistingHandlersÚ_install_handlersÚ_install_loggersÚ _releaseLock)ÚfnameÚdefaultsÚdisable_existing_loggersrÚcpÚ formattersÚhandlers©rú&/usr/lib64/python3.9/logging/config.pyÚ fileConfig3s       rc Csj| d¡}| d¡}t|ƒ}|D]D}|d|}zt||ƒ}Wq tybt|ƒt||ƒ}Yq 0q |S)z)Resolve a dotted name to a global object.Ú.r)ÚsplitÚpopÚ __import__ÚgetattrÚAttributeError)ÚnameÚusedÚfoundÚnrrrÚ_resolveUs    r&cCs ttj|ƒS©N)ÚmapÚstrÚstrip)ÚalistrrrÚ _strip_spacescsr,c Cs®|dd}t|ƒsiS| d¡}t|ƒ}i}|D]v}d|}|j|dddd}|j|d ddd}|j|d dd d}tj}|| d ¡} | r”t| ƒ}||||ƒ} | ||<q2|S) zCreate and return formattersrÚkeysú,z formatter_%sÚformatTN)ÚrawÚfallbackÚdatefmtÚstyleú%Úclass)Úlenrr,Úgetr Ú Formatterr&) rÚflistrÚformÚsectnameÚfsÚdfsÚstlÚcÚ class_nameÚfrrrr fs$    r c Csh|dd}t|ƒsiS| d¡}t|ƒ}i}g}|D] }|d|}|d}| dd¡}zt|ttƒƒ}WnttfyŒt |ƒ}Yn0| dd ¡} t| ttƒƒ} | d d ¡} t| ttƒƒ} || i| ¤Ž} || _ d |vrð|d } |   | ¡t|ƒr|   ||¡t |tjjƒr<| d d¡} t| ƒr<| | | f¡| ||<q6|D]\} }|  ||¡qJ|S)zInstall and return handlersrr-r.z handler_%sr5Ú formatterÚÚargsú()Úkwargsz{}ÚlevelÚtarget)r6rr,r7ÚevalÚvarsr r!Ú NameErrorr&r"ÚsetLevelÚ setFormatterÚ issubclassrÚ MemoryHandlerÚappendÚ setTarget)rrÚhlistrÚfixupsÚhandÚsectionÚklassÚfmtrDrFÚhrGrHÚtrrrr|sD             rcCsTtj}|D]D}|jj|}||vrHt|tjƒsN| tj¡g|_d|_ q ||_ q dS)aå When (re)configuring logging, handle loggers which were in the previous configuration but are not in the new configuration. There's no point deleting them as other threads may continue to hold references to them; and by disabling them, you stop them doing any logging. However, don't disable children of named loggers, as that's probably not what was intended by the user. Also, allow existing loggers to NOT be disabled if disable_existing is false. TN) r ÚrootÚmanagerÚ loggerDictrÚ PlaceHolderrLÚNOTSETrÚ propagateÚdisabled)ÚexistingÚ child_loggersÚdisable_existingrZÚlogÚloggerrrrÚ_handle_existing_loggers¢s    rfcCs |dd}| d¡}tt|ƒƒ}| d¡|d}tj}|}d|vrX|d}| |¡|jdd…D]}| |¡qf|d} t | ƒr°|  d¡} t| ƒ} | D]} |  || ¡qœt|j j   ¡ƒ} |  ¡g} |D](}|d |}|d } |jd d d }t | ¡}| | vrv|  | ¡d }| d}t |ƒ}t | ƒ}||krl| |d|…|kr`|  | |¡|d 7}q2|  | ¡d|vr’|d}| |¡|jdd…D]}| |¡q ||_d|_|d} t | ƒrÐ|  d¡} t| ƒ} | D]} |  || ¡qäqÐt| | |ƒdS)zCreate and install loggersÚloggersr-r.rZÚ logger_rootrGNrz logger_%sÚqualnamer_é)r1rr)rÚlistr,Úremover rZrLrÚ removeHandlerr6Ú addHandlerr[r\r-ÚsortÚgetintÚ getLoggerÚindexrPr_r`rf)rrrcÚllistrUrZrdrGrXrRrTrarbÚqnr_reÚiÚprefixedÚpflenÚ num_existingrrrr¸sd                  rcCs.tj ¡t tjdd…¡tjdd…=dS)z!Clear and close existing handlersN)r Ú _handlersÚclearÚshutdownÚ _handlerListrrrrrs rz^[a-z_][a-z0-9_]*$cCst |¡}|std|ƒ‚dS)Nz!Not a valid Python identifier: %rT)Ú IDENTIFIERÚmatchÚ ValueError)ÚsÚmrrrÚ valid_idents  r‚c@s"eZdZdZddd„Zdd„ZdS) ÚConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j |¡}||ur>|r |||<t|ƒtttfvr>||_||_|Sr')Ú configuratorÚconvertÚtypeÚConvertingDictÚConvertingListÚConvertingTupleÚparentÚkey)Úselfr‹ÚvalueÚreplaceÚresultrrrÚconvert_with_key#s  ÿz ConvertingMixin.convert_with_keycCs0|j |¡}||ur,t|ƒtttfvr,||_|Sr')r„r…r†r‡rˆr‰rŠ)rŒrrrrrr…/s  ÿzConvertingMixin.convertN)T)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr…rrrrrƒ s rƒc@s,eZdZdZdd„Zd dd„Zd dd„ZdS) r‡z A converting dictionary wrapper.cCst ||¡}| ||¡Sr')ÚdictÚ __getitem__r©rŒr‹rrrrr–Ds zConvertingDict.__getitem__NcCst |||¡}| ||¡Sr')r•r7r©rŒr‹Údefaultrrrrr7HszConvertingDict.getcCst |||¡}|j||ddS©NF)rŽ)r•rrr˜rrrrLszConvertingDict.pop)N)N)r‘r’r“r”r–r7rrrrrr‡As r‡c@s"eZdZdZdd„Zddd„ZdS) rˆzA converting list wrapper.cCst ||¡}| ||¡Sr')rkr–rr—rrrr–Rs zConvertingList.__getitem__éÿÿÿÿcCst ||¡}| |¡Sr')rkrr…)rŒÚidxrrrrrVs zConvertingList.popN)r›)r‘r’r“r”r–rrrrrrˆPsrˆc@seZdZdZdd„ZdS)r‰zA converting tuple wrapper.cCst ||¡}|j||ddSrš)Útupler–rr—rrrr–\s zConvertingTuple.__getitem__N)r‘r’r“r”r–rrrrr‰Zsr‰c@sŒeZdZdZe d¡Ze d¡Ze d¡Ze d¡Z e d¡Z ddd œZ e e ƒZd d „Zd d „Zdd„Zdd„Zdd„Zdd„Zdd„ZdS)ÚBaseConfiguratorzI The configurator base class which defines some useful defaults. z%^(?P[a-z]+)://(?P.*)$z ^\s*(\w+)\s*z^\.\s*(\w+)\s*z^\[\s*(\w+)\s*\]\s*z^\d+$Ú ext_convertÚ cfg_convert)ÚextÚcfgcCst|ƒ|_||j_dSr')r‡Úconfigr„)rŒr£rrrÚ__init__us zBaseConfigurator.__init__c Cs¾| d¡}| d¡}z\| |¡}|D]F}|d|7}zt||ƒ}Wq$tyh| |¡t||ƒ}Yq$0q$|WSty¸t ¡dd…\}}td||fƒ}|||_ |_ |‚Yn0dS)z` Resolve strings to objects using standard import and attribute syntax. rrrjNzCannot resolve %r: %s) rrÚimporterr r!Ú ImportErrorÚsysÚexc_inforÚ __cause__Ú __traceback__) rŒr€r"r#r$ÚfragÚeÚtbÚvrrrÚresolveys"       zBaseConfigurator.resolvecCs | |¡S)z*Default converter for the ext:// protocol.)r¯©rŒrrrrrŸszBaseConfigurator.ext_convertcCsü|}|j |¡}|dur&td|ƒ‚nÒ|| ¡d…}|j| ¡d}|rø|j |¡}|rn|| ¡d}nb|j |¡}|rÐ| ¡d}|j |¡s ||}n0zt |ƒ}||}Wnt yÎ||}Yn0|ræ|| ¡d…}qHtd||fƒ‚qH|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) Ú WORD_PATTERNr~rÚendr£ÚgroupsÚ DOT_PATTERNÚ INDEX_PATTERNÚ DIGIT_PATTERNÚintÚ TypeError)rŒrÚrestrÚdrœr%rrrr ”s4        ÿzBaseConfigurator.cfg_convertcCsÐt|tƒs$t|tƒr$t|ƒ}||_n¨t|tƒsHt|tƒrHt|ƒ}||_n„t|tƒsvt|tƒrvt|dƒsvt|ƒ}||_nVt|t ƒrÌ|j   |¡}|rÌ|  ¡}|d}|j  |d¡}|rÌ|d}t||ƒ}||ƒ}|S)zé Convert values to an appropriate type. dicts, lists and tuples are replaced by their converting alternatives. Strings are checked to see if they have a conversion format and are converted if they do. Ú_fieldsÚprefixNÚsuffix)rr‡r•r„rˆrkr‰rr r)ÚCONVERT_PATTERNr~Ú groupdictÚvalue_convertersr7r )rŒrrrºr¼Ú converterr½rrrr…¶s0 ÿÿ   zBaseConfigurator.convertcsnˆ d¡}t|ƒs| |¡}ˆ dd¡}‡fdd„ˆDƒ}|fi|¤Ž}|rj| ¡D]\}}t|||ƒqT|S)z1Configure an object with a user-supplied factory.rErNcsi|]}t|ƒr|ˆ|“qSr©r‚©Ú.0Úk©r£rrÚ Ùóz5BaseConfigurator.configure_custom..)rÚcallabler¯ÚitemsÚsetattr)rŒr£r?ÚpropsrFrr"rrrÆrÚconfigure_customÒs   z!BaseConfigurator.configure_customcCst|tƒrt|ƒ}|S)z0Utility function which converts lists to tuples.)rrkrr°rrrÚas_tupleàs zBaseConfigurator.as_tupleN)r‘r’r“r”ÚreÚcompiler¾r±r´rµr¶rÀÚ staticmethodrr¥r¤r¯rŸr r…rÍrÎrrrrržas"     þ"ržc@s^eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z ddd„Z ddd„Z ddd„Z dS)ÚDictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. c Csä|j}d|vrtdƒ‚|ddkr2td|dƒ‚| dd¡}i}t ¡zˆ|r¦| d|¡}|D]†}|tjvr€td|ƒ‚qdz6tj|}||}| d d ¡}|r´| t |¡¡Wqdt yè} ztd |ƒ| ‚WYd } ~ qdd } ~ 00qd| d |¡} | D]P}z|  || |d ¡Wqüt yJ} ztd|ƒ| ‚WYd } ~ qüd } ~ 00qü| dd ¡} | rÊz|  | d ¡Wn0t y } ztdƒ| ‚WYd } ~ n d } ~ 00n$| dd ¡} t ƒ| d|¡} | D]R}z|  | |¡| |<Wn4t y} ztd|ƒ| ‚WYd } ~ n d } ~ 00qÈ| d|¡}|D]R}z| ||¡||<Wn4t yz} ztd|ƒ| ‚WYd } ~ n d } ~ 00q,| d|¡}g}t|ƒD]x}z | ||¡}||_|||<WnPt y } z6dt| jƒvrê| |¡ntd |ƒ| ‚WYd } ~ n d } ~ 00q˜|D]\}z | ||¡}||_|||<Wn4t yn} ztd |ƒ| ‚WYd } ~ n d } ~ 00qtj} t| jj ¡ƒ}| ¡g}| d |¡} | D]Ä}||vr| |¡d}|d}t|ƒ}t|ƒ}||kr||d |…|kr| ||¡|d7}qÚ| |¡z|  || |¡Wn4t yf} ztd|ƒ| ‚WYd } ~ n d } ~ 00q¦t||| ƒ| dd ¡} | rÊz|  | ¡Wn0t yÈ} ztdƒ| ‚WYd } ~ n d } ~ 00Wt ¡n t ¡0d S)zDo the configuration.Úversionz$dictionary doesn't specify a versionrjzUnsupported version: %sÚ incrementalFrzNo handler found with name %rrGNzUnable to configure handler %rrgTzUnable to configure logger %rrZzUnable to configure root loggerrrz Unable to configure formatter %rÚfilterszUnable to configure filter %rútarget not configured yetr) r£rrr rr7ryrLÚ _checkLevelÚ ExceptionÚconfigure_loggerÚconfigure_rootrÚconfigure_formatterÚconfigure_filterÚsortedÚconfigure_handlerr"r)r©rPrZrkr[r\r-rorrr6rlrfr)rŒr£rÔÚ EMPTY_DICTrr"ÚhandlerÚhandler_configrGr¬rgrZrcrrÕÚdeferredrarbrurvrwrxrrrÚ configureìs    ÿ  ÿÿ ÿÿ ÿ  ÿ ÿÿ ÿÿ    ÿÿ ÿÿ      ÿÿÿ ÿzDictConfigurator.configurec Csäd|vrt|d}z| |¡}Wqàtyp}z:dt|ƒvr<‚| d¡|d<||d<| |¡}WYd}~qàd}~00nl| dd¡}| dd¡}| dd¡}| d d¡}|s°tj} nt|ƒ} d |vrÔ| ||||d ƒ}n | |||ƒ}|S) z(Configure a formatter from a dictionary.rEz'format'r/rWNr2r3r4r5Úvalidate)rÍr¸r)rr7r r8r&) rŒr£ÚfactoryrÚterWÚdfmtr3Úcnamer?rrrrÛŠs* "     z$DictConfigurator.configure_formattercCs.d|vr| |¡}n| dd¡}t |¡}|S)z%Configure a filter from a dictionary.rEr"rC)rÍr7r ÚFilter)rŒr£rr"rrrrÜ®s    z!DictConfigurator.configure_filterc CsZ|D]P}z| |jd|¡WqtyR}ztd|ƒ|‚WYd}~qd}~00qdS)z/Add filters to a filterer from a list of names.rÕzUnable to add filter %rN)Ú addFilterr£rØr)rŒÚfiltererrÕrAr¬rrrÚ add_filters·s zDictConfigurator.add_filtersc s’tˆƒ}ˆ dd¡}|r^z|jd|}Wn2ty\}ztd|ƒ|‚WYd}~n d}~00ˆ dd¡}ˆ dd¡}dˆvr¢ˆ d¡}t|ƒsš| |¡}|}nˆ d¡} | | ¡} t| tj j ƒrJd ˆvrJz>|jd ˆd } t | tj ƒsˆ  |¡td ƒ‚| ˆd <Wn8tyF}ztd ˆd ƒ|‚WYd}~n d}~00nZt| tj jƒrxd ˆvrx| ˆd ¡ˆd <n,t| tj jƒr¤dˆvr¤| ˆd¡ˆd<| }ˆ dd¡} ‡fdd„ˆDƒ} z|fi| ¤Ž}WnRty*}z8dt|ƒvrú‚|  d¡| d<|fi| ¤Ž}WYd}~n d}~00|r<| |¡|durV| t |¡¡|rh| ||¡| rŽ|  ¡D]\}}t|||ƒqv|S)z&Configure a handler from a dictionary.rBNrzUnable to set formatter %rrGrÕrEr5rHrrÖzUnable to set target handler %rÚmailhostÚaddressrcsi|]}t|ƒr|ˆ|“qSrrÂrÃrÆrrrÇçrÈz6DictConfigurator.configure_handler..z'stream'ÚstreamÚstrm)r•rr£rØrrÉr¯rNr rrOrÚHandlerÚupdater¸Ú SMTPHandlerrÎÚ SysLogHandlerr)rMrLr×rìrÊrË)rŒr£Ú config_copyrBr¬rGrÕr?rårèrVÚthrÌrFrrær"rrrÆrrÞ¿s~ ÿÿ      ÿ  ÿÿÿÿ $   z"DictConfigurator.configure_handlerc CsZ|D]P}z| |jd|¡WqtyR}ztd|ƒ|‚WYd}~qd}~00qdS)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)rnr£rØr)rŒrerrXr¬rrrÚ add_handlersþs zDictConfigurator.add_handlersFcCs‚| dd¡}|dur$| t |¡¡|s~|jdd…D]}| |¡q6| dd¡}|rb| ||¡| dd¡}|r~| ||¡dS)zU Perform configuration which is common to root and non-root loggers. rGNrrÕ)r7rLr r×rrmr÷rì)rŒrer£rÔrGrXrrÕrrrÚcommon_logger_configs     z%DictConfigurator.common_logger_configcCs6t |¡}| |||¡| dd¡}|dur2||_dS)z.Configure a non-root logger from a dictionary.r_N)r rqrør7r_)rŒr"r£rÔrer_rrrrÙs   z!DictConfigurator.configure_loggercCst ¡}| |||¡dS)z*Configure a root logger from a dictionary.N)r rqrø)rŒr£rÔrZrrrrÚ szDictConfigurator.configure_rootN)F)F)F) r‘r’r“r”rãrÛrÜrìrÞr÷rørÙrÚrrrrrÒæs$ ?  rÒcCst|ƒ ¡dS)z%Configure logging using a dictionary.N)ÚdictConfigClassrãrÆrrrÚ dictConfig'srúcsDGdd„dtƒ}Gdd„dtƒ}G‡fdd„dtjƒ‰ˆ||||ƒS)au Start up a socket server on the specified port, and listen for new configurations. These will be sent as a file suitable for processing by fileConfig(). Returns a Thread object on which you can call start() to start the server, and which you can join() when appropriate. To stop the server, call stopListening(). Use the ``verify`` argument to verify any bytes received across the wire from a client. If specified, it should be a callable which receives a single argument - the bytes of configuration data received across the network - and it should return either ``None``, to indicate that the passed in bytes could not be verified and should be discarded, or a byte string which is then passed to the configuration machinery as normal. Note that you can return transformed bytes, e.g. by decrypting the bytes passed in. c@seZdZdZdd„ZdS)z#listen..ConfigStreamHandlerz¤ Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c SsPz|j}| d¡}t|ƒdkrt d|¡d}|j |¡}t|ƒ|krb|| |t|ƒ¡}q>|jjdurz|j |¡}|durþ| d¡}z,ddl}|  |¡}t |t ƒs®J‚t |ƒWnDt yüt |¡}z t|ƒWnt yöt ¡Yn0Yn0|jjr|jj ¡Wn4tyJ}z|jtkr6‚WYd}~n d}~00dS)zè Handle a request. Each request is expected to be a 4-byte length, packed using struct.pack(">L", n), followed by the config file. Uses fileConfig() to do the grunt work. éz>LrNzutf-8)Ú connectionÚrecvr6ÚstructÚunpackÚserverÚverifyÚdecodeÚjsonÚloadsrr•rúrØÚioÚStringIOrÚ tracebackÚ print_excÚreadyÚsetÚOSErrorÚerrnoÚ RESET_ERROR)rŒÚconnÚchunkÚslenrrºÚfiler¬rrrÚhandleGs8              z*listen..ConfigStreamHandler.handleN)r‘r’r“r”rrrrrÚConfigStreamHandler@src@s0eZdZdZdZdedddfdd„Zdd„ZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rjÚ localhostNcSs>t |||f|¡t ¡d|_t ¡d|_||_||_dS)Nrrj) rr¤r rÚabortrÚtimeoutr r)rŒÚhostÚportràr rrrrr¤usz-listen..ConfigSocketReceiver.__init__cSs`ddl}d}|sT| |j ¡ggg|j¡\}}}|r<| ¡t ¡|j}t ¡q |  ¡dS)Nr) ÚselectÚsocketÚfilenorÚhandle_requestr rrrÚ server_close)rŒrrÚrdÚwrÚexrrrÚserve_until_stoppedsþ  z8listen..ConfigSocketReceiver.serve_until_stopped)r‘r’r“r”Úallow_reuse_addressÚDEFAULT_LOGGING_CONFIG_PORTr¤r!rrrrÚConfigSocketReceiverns ÿ r$cs&eZdZ‡‡fdd„Zdd„Z‡ZS)zlisten..Servercs4tˆ|ƒ ¡||_||_||_||_t ¡|_dSr') Úsuperr¤ÚrcvrÚhdlrrrÚ threadingÚEventr )rŒr&r'rr)ÚServerÚ __class__rrr¤s zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr0|jd|_|j ¡t ¡|a t  ¡|  ¡dS)N)rràr rrrj) r&rr'r rÚserver_addressr r rÚ _listenerrr!)rŒrrrrÚrun—s þ   zlisten..Server.run)r‘r’r“r¤r.Ú __classcell__r©r*)r+rr*sr*)rrr(ÚThread)rrrr$rr0rÚlisten,s.r2cCs2t ¡ztrdt_daWt ¡n t ¡0dS)zN Stop the listening server which was created with a call to listen(). rjN)r rr-rrrrrrÚ stopListening¦s r3)NT)+r”r rr Úlogging.handlersrÏrþr§r(rÚ socketserverrrr#Ú ECONNRESETr r-rr&r,r rrfrrrÐÚIr}r‚Úobjectrƒr•r‡rkrˆrr‰ržrÒrùrúr2r3rrrrÚsH  "&W! Az