U e5dŽã@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 |||ƒW5t  ¡XdS)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Ú isinstanceZRawConfigParserZ ConfigParserÚhasattrZ read_fileÚreadÚ_create_formattersÚloggingÚ _acquireLockÚ _releaseLockÚ_clearExistingHandlersÚ_install_handlersÚ_install_loggers)ZfnameÚdefaultsÚdisable_existing_loggersrÚcpÚ formattersÚhandlers©rú&/usr/lib64/python3.8/logging/config.pyÚ fileConfig3s       rc Csl| d¡}| d¡}t|ƒ}|D]F}|d|}zt||ƒ}Wq tk rdt|ƒt||ƒ}Yq Xq |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)ZalistrrrÚ _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") rZflistrZformZsectnameZfsZdfsZstlÚcÚ class_nameÚfrrrr fs$    r c Cs^|dd}t|ƒsiS| d¡}t|ƒ}i}g}|D]}|d|}|d}| dd¡}zt|ttƒƒ}Wn ttfk rŽt |ƒ}YnX| dd ¡} t| ttƒƒ} | d d ¡} t| ttƒƒ} || | Ž} d |krè|d } |   | ¡t|ƒrþ|   ||¡t |tj jƒr2| d d¡} t| ƒr2| | | f¡| ||<q6|D]\} }|  ||¡q@|S)zInstall and return handlersrr(r)z handler_%sr0Ú formatterÚÚargsú()Úkwargsz{}ÚlevelÚtarget)r1rr'r2ÚevalÚvarsr rÚ NameErrorr"ÚsetLevelÚ setFormatterÚ issubclassrÚ MemoryHandlerÚappendZ setTarget)rrÚhlistrZfixupsÚhandÚsectionÚklassÚfmtr9r;Úhr<r=Útrrrr|sB             rcCsTtj}|D]D}|jj|}||krHt|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Ú loggerDictrZ PlaceHolderrAZNOTSETrÚ propagateÚdisabled)ÚexistingÚ child_loggersÚdisable_existingrMÚlogÚloggerrrrÚ_handle_existing_loggers¡s    rWcCs |dd}| d¡}tt|ƒƒ}| d¡|d}tj}|}d|krX|d}| |¡|jdd…D]}| |¡qf|d} t | ƒr°|  d¡} t| ƒ} | D]} |  || ¡qœt|j j   ¡ƒ} |  ¡g} |D](}|d |}|d } |jd d d }t | ¡}| | krv|  | ¡d }| d}t |ƒ}t | ƒ}||krl| |d|…|kr`|  | |¡|d 7}q2|  | ¡d|kr’|d}| |¡|jdd…D]}| |¡q ||_d|_|d} t | ƒrÐ|  d¡} t| ƒ} | D]} |  || ¡qäqÐt| | |ƒdS)zCreate and install loggersÚloggersr(r)rMZ logger_rootr<Nrz logger_%sÚqualnamerPé)r,rr)rÚlistr'Úremover rMrArÚ removeHandlerr1Ú addHandlerrNrOr(ÚsortZgetintÚ getLoggerÚindexrErPrQrW)rrrTZllistrHrMrUr<rKrFrGrRrSZqnrPrVÚiÚprefixedÚpflenÚ num_existingrrrr·sd                  rcCs.tj ¡t tjdd…¡tjdd…=dS)z!Clear and close existing handlersN)r Ú _handlersÚclearZshutdownZ _handlerListrrrrr s r z^[a-z_][a-z0-9_]*$cCst |¡}|std|ƒ‚dS)Nz!Not a valid Python identifier: %rT)Ú IDENTIFIERÚmatchÚ ValueError)ÚsÚmrrrÚ valid_idents  rmc@s"eZdZdZddd„Zdd„ZdS) ÚConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j |¡}||k r>|r |||<t|ƒtttfkr>||_||_|Sr#)Ú configuratorÚconvertÚtypeÚConvertingDictÚConvertingListÚConvertingTupleÚparentÚkey)ÚselfrvÚvalueÚreplaceÚresultrrrÚconvert_with_key"s  ÿz ConvertingMixin.convert_with_keycCs0|j |¡}||k r,t|ƒtttfkr,||_|Sr#)rorprqrrrsrtru)rwrxrzrrrrp.s  ÿzConvertingMixin.convertN)T)Ú__name__Ú __module__Ú __qualname__Ú__doc__r{rprrrrrns rnc@s,eZdZdZdd„Zd dd„Zd dd„ZdS) rrz A converting dictionary wrapper.cCst ||¡}| ||¡Sr#)ÚdictÚ __getitem__r{©rwrvrxrrrrCs zConvertingDict.__getitem__NcCst |||¡}| ||¡Sr#)r€r2r{©rwrvÚdefaultrxrrrr2GszConvertingDict.getcCst |||¡}|j||ddS©NF)ry)r€rr{rƒrrrrKszConvertingDict.pop)N)N)r|r}r~rrr2rrrrrrr@s rrc@s"eZdZdZdd„Zddd„ZdS) rszA converting list wrapper.cCst ||¡}| ||¡Sr#)r[rr{r‚rrrrQs zConvertingList.__getitem__éÿÿÿÿcCst ||¡}| |¡Sr#)r[rrp)rwÚidxrxrrrrUs zConvertingList.popN)r†)r|r}r~rrrrrrrrsOsrsc@seZdZdZdd„ZdS)rtzA converting tuple wrapper.cCst ||¡}|j||ddSr…)Útuplerr{r‚rrrr[s zConvertingTuple.__getitem__N)r|r}r~rrrrrrrtYsrtc@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)ZextZcfgcCst|ƒ|_||j_dSr#)rrÚconfigro)rwrŒrrrÚ__init__ts zBaseConfigurator.__init__c CsÂ| d¡}| d¡}z^| |¡}|D]H}|d|7}zt||ƒ}Wq$tk rj| |¡t||ƒ}Yq$Xq$|WStk r¼t ¡dd…\}}td||fƒ}|||_ |_ |‚YnXdS)z` Resolve strings to objects using standard import and attribute syntax. rrrZNzCannot resolve %r: %s) rrÚimporterrrÚ ImportErrorÚsysÚexc_inforjÚ __cause__Ú __traceback__) rwrkrrr ZfragÚeÚtbÚvrrrÚresolvexs"     zBaseConfigurator.resolvecCs | |¡S)z*Default converter for the ext:// protocol.)r—©rwrxrrrrŠszBaseConfigurator.ext_convertcCsþ|}|j |¡}|dkr&td|ƒ‚nÔ|| ¡d…}|j| ¡d}|rú|j |¡}|rn|| ¡d}nd|j |¡}|rÒ| ¡d}|j |¡s ||}n2zt |ƒ}||}Wnt k rÐ||}YnX|rè|| ¡d…}qHtd||fƒ‚qH|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) Ú WORD_PATTERNrirjÚendrŒÚgroupsÚ DOT_PATTERNÚ INDEX_PATTERNÚ DIGIT_PATTERNÚintÚ TypeError)rwrxÚrestrlÚ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)rrrr€rorsr[rtrˆrr%ÚCONVERT_PATTERNriÚ groupdictÚvalue_convertersr2r)rwrxrlr¢r¤Z converterr¥rrrrpµs0 ÿÿ   zBaseConfigurator.convertcsjˆ d¡}t|ƒs| |¡}ˆ dd¡}‡fdd„ˆDƒ}|f|Ž}|rf| ¡D]\}}t|||ƒqP|S)z1Configure an object with a user-supplied factory.r:rNcsi|]}t|ƒr|ˆ|“qSr©rm©Ú.0Úk©rŒrrÚ Øsz5BaseConfigurator.configure_custom..)rÚcallabler—ÚitemsÚsetattr)rwrŒr4Úpropsr;rzrrxrr­rÚconfigure_customÑs    z!BaseConfigurator.configure_customcCst|tƒrt|ƒ}|S)z0Utility function which converts lists to tuples.)rr[rˆr˜rrrÚas_tupleßs zBaseConfigurator.as_tupleN)r|r}r~rÚreÚcompiler¦r™rœrržr¨Ú staticmethodrrŽrr—rŠr‹rpr³r´rrrrr‰`s"     þ"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|krtdƒ‚|ddkr2td|dƒ‚| dd¡}i}t ¡zn|r | d|¡}|D]„}|tjkr€td|ƒ‚qdz6tj|}||}| d d ¡}|r´| t  |¡¡Wqdt k ræ} ztd |ƒ| ‚W5d } ~ XYqdXqd| d |¡} | D]N}z|  || |d ¡Wqút k rF} ztd|ƒ| ‚W5d } ~ XYqúXqú| dd ¡} | r¸z|  | d ¡Wn.t k rš} ztdƒ| ‚W5d } ~ XYnXn| dd ¡} t ƒ| d|¡} | D]P}z| | |¡| |<Wn2t k r} ztd|ƒ| ‚W5d } ~ XYnXqÂ| d|¡}|D]P}z| ||¡||<Wn2t k rp} ztd|ƒ| ‚W5d } ~ XYnXq$| d|¡}g}t|ƒD]v}z | ||¡}||_|||<WnNt k r} z.dt| jƒkrâ| |¡ntd |ƒ| ‚W5d } ~ XYnXqŽ|D]Z}z | ||¡}||_|||<Wn2t k r`} ztd |ƒ| ‚W5d } ~ XYnXq tj} t| jj ¡ƒ}| ¡g}| d |¡} | D]Â}||kr| |¡d}|d}t|ƒ}t|ƒ}||kr||d |…|krú| ||¡|d7}qÌ| |¡z|  || |¡Wn2t k rV} ztd|ƒ| ‚W5d } ~ XYnXq˜t||| ƒ| dd ¡} | r¸z|  | ¡Wn.t k r¶} ztdƒ| ‚W5d } ~ XYnXW5t ¡Xd S)zDo the configuration.Úversionz$dictionary doesn't specify a versionrZzUnsupported version: %sÚ incrementalFrzNo handler found with name %rr<NzUnable to configure handler %rrXTzUnable to configure logger %rrMzUnable to configure root loggerrrz Unable to configure formatter %rÚfilterszUnable to configure filter %rútarget not configured yetr) rŒrjrr r r r2rfrAÚ _checkLevelÚ ExceptionÚconfigure_loggerÚconfigure_rootr Úconfigure_formatterÚconfigure_filterÚsortedÚconfigure_handlerrr%r’rErMr[rNrOr(r_rar1r\rW)rwrŒrºZ EMPTY_DICTrrÚhandlerZhandler_configr<r”rXrMrTrr»ZdeferredrRrSrbrcrdrerrrÚ configureës    ÿ  ÿÿ ÿÿ ÿ  ÿ ÿÿ ÿÿ    ÿÿ ÿÿ      ÿÿÿ ÿzDictConfigurator.configurec Csâd|krr|d}z| |¡}WqÞtk rn}z2dt|ƒkr>‚| d¡|d<||d<| |¡}W5d}~XYqÞXnl| dd¡}| dd¡}| dd¡}| d d¡}|s®tj} nt|ƒ} d |krÒ| ||||d ƒ}n | |||ƒ}|S) z(Configure a formatter from a dictionary.r:z'format'r*rJNr-r.r/r0Zvalidate)r³r r%rr2r r3r") rwrŒÚfactoryrzÚterJZdfmtr.Úcnamer4rrrrÁ‰s*      z$DictConfigurator.configure_formattercCs.d|kr| |¡}n| dd¡}t |¡}|S)z%Configure a filter from a dictionary.r:rr8)r³r2r ZFilter)rwrŒrzrrrrr­s    z!DictConfigurator.configure_filterc CsX|D]N}z| |jd|¡Wqtk rP}ztd|ƒ|‚W5d}~XYqXqdS)z/Add filters to a filterer from a list of names.r»zUnable to add filter %rN)Z addFilterrŒr¾rj)rwZfiltererr»r6r”rrrÚ add_filters¶s zDictConfigurator.add_filtersc s„tˆƒ}ˆ dd¡}|r\z|jd|}Wn0tk rZ}ztd|ƒ|‚W5d}~XYnXˆ dd¡}ˆ dd¡}dˆkr ˆ d¡}t|ƒs˜| |¡}|}nˆ d¡} | | ¡} t| tj j ƒrFd ˆkrFz>|jd ˆd } t | tj ƒsˆ  |¡td ƒ‚| ˆd <Wn6tk rB}ztd ˆd ƒ|‚W5d}~XYnXnZt| tj jƒrtd ˆkrt| ˆd ¡ˆd <n,t| tj jƒr dˆkr | ˆd¡ˆd<| }ˆ dd¡} ‡fdd„ˆDƒ} z|f| Ž}WnLtk r}z,dt|ƒkrô‚|  d¡| d<|f| Ž}W5d}~XYnX|r.| |¡|dk rH| t |¡¡|rZ| ||¡| r€|  ¡D]\}}t|||ƒqh|S)z&Configure a handler from a dictionary.r7NrzUnable to set formatter %rr<r»r:r0r=rr¼zUnable to set target handler %rZmailhostZaddressrcsi|]}t|ƒr|ˆ|“qSrr©rªr­rrr®æsz6DictConfigurator.configure_handler..z'stream'ÚstreamZstrm)r€rrŒr¾rjr¯r—rCr rrDrZHandlerÚupdater Z SMTPHandlerr´Z SysLogHandlerr%rBrAr½rÊr°r±)rwrŒZ config_copyr7r”r<r»r4rÇrÉrIZthr²r;rzrÈrrxrr­rrľs~ ÿÿ      ÿ  ÿÿÿÿ    z"DictConfigurator.configure_handlerc CsX|D]N}z| |jd|¡Wqtk rP}ztd|ƒ|‚W5d}~XYqXqdS)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)r^rŒr¾rj)rwrVrrKr”rrrÚ add_handlersýs zDictConfigurator.add_handlersFcCs‚| dd¡}|dk r$| t |¡¡|s~|jdd…D]}| |¡q6| dd¡}|rb| ||¡| dd¡}|r~| ||¡dS)zU Perform configuration which is common to root and non-root loggers. r<Nrr»)r2rAr r½rr]rÍrÊ)rwrVrŒrºr<rKrr»rrrÚcommon_logger_configs     z%DictConfigurator.common_logger_configcCs6t |¡}| |||¡| dd¡}|dk r2||_dS)z.Configure a non-root logger from a dictionary.rPN)r r`rÎr2rP)rwrrŒrºrVrPrrrr¿s   z!DictConfigurator.configure_loggercCst ¡}| |||¡dS)z*Configure a root logger from a dictionary.N)r r`rÎ)rwrŒrºrMrrrrÀszDictConfigurator.configure_rootN)F)F)F) r|r}r~rrÆ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 SsVz|j}| d¡}t|ƒdkrt d|¡d}|j |¡}t|ƒ|krb|| |t|ƒ¡}q>|jjdk rz|j |¡}|dk r| d¡}z,ddl}|  |¡}t |t ƒs°t ‚t |ƒWnJtk rt |¡}z t|ƒWntk rþt ¡YnXYnX|jjr|jj ¡Wn2tk rP}z|jtkr@‚W5d}~XYnXdS)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)Z connectionZrecvr1ÚstructZunpackÚserverÚverifyÚdecodeÚjsonÚloadsrr€ÚAssertionErrorrÐr¾ÚioÚStringIOrÚ tracebackÚ print_excÚreadyÚsetÚOSErrorÚerrnoÚ RESET_ERROR)rwZconnÚchunkZslenrÖr¢Úfiler”rrrÚhandleFs8             z*listen..ConfigStreamHandler.handleN)r|r}r~rrärrrrÚConfigStreamHandler?sråc@s0eZdZdZdZdedddfdd„Zdd„ZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rZZ localhostNcSs>t |||f|¡t ¡d|_t ¡d|_||_||_dS)NrrZ) rrr r Úabortr ÚtimeoutrÝrÔ)rwZhostÚportrÅrÝrÔrrrrtsz-listen..ConfigSocketReceiver.__init__cSs`ddl}d}|sT| |j ¡ggg|j¡\}}}|r<| ¡t ¡|j}t ¡q |  ¡dS)Nr) ÚselectZsocketÚfilenorçZhandle_requestr r rær Z server_close)rwréræZrdZwrZexrrrÚserve_until_stopped~sþ  z8listen..ConfigSocketReceiver.serve_until_stopped)r|r}r~rZallow_reuse_addressÚDEFAULT_LOGGING_CONFIG_PORTrrërrrrÚConfigSocketReceivermsÿ rícs&eZdZ‡‡fdd„Zdd„Z‡ZS)zlisten..Servercs4tˆ|ƒ ¡||_||_||_||_t ¡|_dSr#) ÚsuperrÚrcvrÚhdlrrèrÔÚ threadingZEventrÝ)rwrïrðrèrÔ)ÚServerÚ __class__rrrŽs zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr0|jd|_|j ¡t ¡|a t  ¡|  ¡dS)N)rèrÅrÝrÔrrZ) rïrèrðrÝrÔZserver_addressrÞr r Ú _listenerr rë)rwrÓrrrÚrun–s þ   zlisten..Server.run)r|r}r~rrõÚ __classcell__r©rò)rórròŒsrò)rrrñZThread)rèrÔrårírr÷rÚlisten+s.røcCs*t ¡ztrdt_daW5t ¡XdS)zN Stop the listening server which was created with a call to listen(). rZN)r r r rôrærrrrÚ stopListening¥s rù)NT)+rràrÙr Zlogging.handlersrµrÒrrñrÛZ socketserverrrrìZ ECONNRESETrárôrr"r'r rrWrr r¶ÚIrhrmÚobjectrnr€rrr[rsrˆrtr‰r¸rÏrÐrørùrrrrÚsH  "%W! Az