o w[e@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z dZ ejZdad+ddZdd Zd d Zd d ZddZddZddZddZedejZddZGdddeZGdddeeZ Gddde!eZ"Gdd d e#eZ$Gd!d"d"eZ%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)ThreadingTCPServerStreamRequestHandleriF#TcCsddl}t||jr |}n||}t|dr||n t|}|j||dt |}t zt t ||}t|||Wt dSt w)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). rNreadline)encoding) configparser isinstanceRawConfigParser ConfigParserhasattr read_fileio text_encodingread_create_formatterslogging _acquireLock_clearExistingHandlers_install_handlers_install_loggers _releaseLock)fnamedefaultsdisable_existing_loggersrrcp formattershandlersr%/usr/lib/python3.10/logging/config.py fileConfig3s      rc Csh|d}|d}t|}|D]!}|d|}zt||}Wqty1t|t||}Yqw|S)z)Resolve a dotted name to a global object..r)splitpop __import__getattrAttributeError)nameusedfoundnrrr_resolveVs    r)cCs ttj|SN)mapstrstrip)alistrrr _strip_spacesds r/c Cs|dd}t|s iS|d}t|}i}|D];}d|}|j|dddd}|j|d ddd}|j|d dd d}tj}||d } | rJt| }||||} | ||<q|S) zCreate and return formattersrkeys,z formatter_%sformatTN)rawfallbackdatefmtstyle%class)lenr r/getr Formatterr)) rflistrformsectnamefsdfsstlc class_namefrrrrgs$    rc Cs^|dd}t|s iS|d}t|}i}g}|D]}|d|}|d}|dd}z t|tt}WnttfyEt |}Ynw|dd } t| tt} |d d } t| tt} || i| } || _ d |vrw|d } | | t|r| ||t |tjjr|d d} t| r|| | f| ||<q|D] \} }| ||q|S)zInstall and return handlersrr0r1z handler_%sr8 formatterargs()kwargsz{}leveltarget)r9r r/r:evalvarsrr$ NameErrorr)r%setLevel setFormatter issubclassr MemoryHandlerappend setTarget)rrhlistrfixupshandsectionklassfmtrGrIhrJrKtrrrr}sF           rcCsTtj}|D]"}|jj|}||vr$t|tjs#|tjg|_d|_ q||_ qdS)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) rrootmanager loggerDictr PlaceHolderrONOTSETr propagatedisabled)existing child_loggersdisable_existingr]logloggerrrr_handle_existing_loggerss    ricCs|dd}|d}tt|}|d|d}tj}|}d|vr,|d}|||jddD]}||q3|d} t | rX| d} t| } | D] } | || qNt|j j } | g} |D]}|d |}|d } |jd d d }t| }| | vr| | d }| d}t |}t | }||kr| |d||kr| | ||d 7}||ks| | d|vr|d}|||jddD]}||q||_d|_|d} t | r| d} t| } | D] } | || qqht| | |dS)zCreate and install loggersloggersr0r1r] logger_rootrJNrz logger_%squalnamerb)r4rr)r listr/removerr]rOr removeHandlerr9 addHandlerr^r_r0sortgetint getLoggerindexrSrbrcri)rrrfllistrXr]rgrJr[rUrWrdreqnrbrhiprefixedpflen num_existingrrrrsh              rcCs.tjttjddtjdd=dS)z!Clear and close existing handlersN)r _handlersclearshutdown _handlerListrrrrrs rz^[a-z_][a-z0-9_]*$cCst|}|s td|dS)Nz!Not a valid Python identifier: %rT) IDENTIFIERmatch ValueError)smrrr valid_idents  rc@s"eZdZdZdddZddZdS) ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j|}||ur|r|||<t|tttfvr||_||_|Sr*) configuratorconverttypeConvertingDictConvertingListConvertingTupleparentkey)selfrvaluereplaceresultrrrconvert_with_key$s  z ConvertingMixin.convert_with_keycCs0|j|}||urt|tttfvr||_|Sr*)rrrrrrr)rrrrrrr0s  zConvertingMixin.convertN)T)__name__ __module__ __qualname____doc__rrrrrrr!s  rc@s,eZdZdZddZd ddZd ddZdS) rz A converting dictionary wrapper.cCt||}|||Sr*)dict __getitem__rrrrrrrrE  zConvertingDict.__getitem__NcCst|||}|||Sr*)rr:rrrdefaultrrrrr:Is zConvertingDict.getcCst|||}|j||ddSNF)r)rr!rrrrrr!MszConvertingDict.popr*)rrrrrr:r!rrrrrBs  rc@s"eZdZdZddZdddZdS) rzA converting list wrapper.cCrr*)rnrrrrrrrSrzConvertingList.__getitem__cCst||}||Sr*)rnr!r)ridxrrrrr!Ws  zConvertingList.popN)r)rrrrrr!rrrrrQsrc@eZdZdZddZdS)rzA converting tuple wrapper.cCst||}|j||ddSr)tuplerrrrrrr]s zConvertingTuple.__getitem__N)rrrrrrrrrr[s rc@seZdZdZedZedZedZedZ edZ ddd Z e e Zd d Zd d ZddZddZddZddZddZdS)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)extcfgcCst||_||j_dSr*)rconfigr)rrrrr__init__vs  zBaseConfigurator.__init__c Cs|d}|d}z-||}|D]"}|d|7}zt||}Wqty4||t||}Yqw|WStyYtdd\}}td||f}|||_ |_ |w)z` Resolve strings to objects using standard import and attribute syntax. rrrmNzCannot resolve %r: %s) r r!importerr#r$ ImportErrorsysexc_infor __cause__ __traceback__) rrr%r&r'fragetbvrrrresolvezs&       zBaseConfigurator.resolvecCs ||S)z*Default converter for the ext:// protocol.)rrrrrrrs zBaseConfigurator.ext_convertcCs|}|j|}|durtd|||d}|j|d}|r||j|}|r6||d}n1|j|}|rg|d}|j|sO||}nz t |}||}Wn t yf||}Ynw|rr||d}ntd||f|s%|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) WORD_PATTERNrrendrgroups DOT_PATTERN INDEX_PATTERN DIGIT_PATTERNint TypeError)rrrestrdrr(rrrrs8          zBaseConfigurator.cfg_convertcCst|tst|trt|}||_|St|ts&t|tr&t|}||_|St|ts>t|tr>t|ds>t|}||_|St|t ri|j |}|ri| }|d}|j |d}|ri|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. _fieldsprefixNsuffix)rrrrrrnrrr r,CONVERT_PATTERNr groupdictvalue_convertersr:r#)rrrrr converterrrrrrs6   zBaseConfigurator.convertcsnd}t|s||}dd}fddD}|di|}|r5|D] \}}t|||q*|S)z1Configure an object with a user-supplied factory.rHrNci|] }t|r||qSrr.0krrr z5BaseConfigurator.configure_custom..r)r!callableritemssetattr)rrrBpropsrIrr%rrrrconfigure_customs   z!BaseConfigurator.configure_customcCst|tr t|}|S)z0Utility function which converts lists to tuples.)rrnrrrrras_tuples zBaseConfigurator.as_tupleN)rrrrrecompilerrrrrr staticmethodr"rrrrrrrrrrrrrbs$     " rc@s^eZdZdZddZddZddZdd Zd d Zd d Z dddZ dddZ dddZ dS)DictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. c Cs|j}d|vr td|ddkrtd|d|dd}i}tz |r|d|}|D];}|tjvr>td|ztj|}||}|d d }|rX|t|Wq1t yl} ztd || d } ~ ww|d |} | D] }z | || |d Wqut y} ztd|| d } ~ ww|dd } | rz | | d Wnt y} ztd| d } ~ wwnp|dd } t |d|} | D] }z | | || |<Wqt y} ztd|| d } ~ ww|d|}|D]!}z |||||<Wqt y} ztd|| d } ~ ww|d|}g}t|D]<}z|||}||_|||<Wq#t y_} zdt| jvrM||ntd || WYd } ~ q#d } ~ ww|D]'}z|||}||_|||<Wqbt y} ztd || d } ~ wwtj} t| jj}|g}|d |} | D]^}||vr||d}|d}t|}t|}||kr||d ||kr||||d7}||ks||z | || |Wqt y} ztd|| d } ~ wwt||| |dd } | r9z| | Wn't y*} ztd| d } ~ wwWtd SWtd SWtd SWtd Stw)zDo the configuration.versionz$dictionary doesn't specify a versionrmzUnsupported version: %s incrementalFrzNo handler found with name %rrJNzUnable to configure handler %rrjTzUnable to configure logger %rr]zUnable to configure root loggerrrz Unable to configure formatter %rfilterszUnable to configure filter %rtarget not configured yetr) rrr!rrr:r|rO _checkLevel Exceptionconfigure_loggerconfigure_rootrconfigure_formatterconfigure_filtersortedconfigure_handlerr%r,rrSr]rnr^r_r0rrrur9rorir)rrr EMPTY_DICTrr%handlerhandler_configrJrrjr]rfrrdeferredrdrerxryrzr{rrr configuresB                     x v zDictConfigurator.configurec Csd|vr;|d}z||}W|Sty:}zdt|vr|d|d<||d<||}WYd}~|Sd}~ww|dd}|dd}|dd}|d d}|sYtj} nt|} d |vrl| ||||d }|S| |||}|S) z(Configure a formatter from a dictionary.rHz'format'r2rZNr5r6r7r8validate)rrr,r!r:rr;r)) rrfactoryrterZdfmtr6cnamerBrrrrs4      z$DictConfigurator.configure_formattercCs0d|vr ||}|S|dd}t|}|S)z%Configure a filter from a dictionary.rHr%rF)rr:rFilter)rrrr%rrrrs   z!DictConfigurator.configure_filterc CL|D]!}z ||jd|Wqty#}ztd||d}~wwdS)z/Add filters to a filterer from a list of names.rzUnable to add filter %rN) addFilterrrr)rfiltererrrDrrrr add_filterszDictConfigurator.add_filtersc sbt}dd}|r)z |jd|}Wnty(}ztd||d}~wwdd}dd}dvrJd}t|sG||}|}ntd} || } t| tj j rd vrz|jd d } t | tj sx |td | d <Wn>ty}z td d |d}~wwt| tj jrd vr|d d <nt| tj jrdvr|dd<| }dd} fddD} z |di| }Wn'ty}zdt|vr| d| d<|di| }WYd}~nd}~ww|r|||dur|t||r|||| r/| D] \}}t|||q#|S)z&Configure a handler from a dictionary.rENrzUnable to set formatter %rrJrrHr8rKrrzUnable to set target handler %rmailhostaddressrcrrrrrrrrrz6DictConfigurator.configure_handler..z'stream'streamstrmr)rr!rrrrrrQrrrRrHandlerupdater SMTPHandlerr SysLogHandlerr,rPrOrrrr)rr config_copyrErrJrrBrrrYthrrIrrr%rrrrrs               z"DictConfigurator.configure_handlerc Cr)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)rqrrr)rrhrr[rrrr add_handlersrzDictConfigurator.add_handlersFcCs|dd}|dur|t||sA|jddD]}||q|dd}|r1||||dd}|rC|||dSdSdS)zU Perform configuration which is common to root and non-root loggers. rJNrr)r:rOrrrrprr)rrhrrrJr[rrrrrcommon_logger_configs     z%DictConfigurator.common_logger_configcCs@t|}||||d|_|dd}|dur||_dSdS)z.Configure a non-root logger from a dictionary.FrbN)rrtrrcr:rb)rr%rrrhrbrrrrs   z!DictConfigurator.configure_loggercCst}||||dS)z*Configure a root logger from a dictionary.N)rrtr)rrrr]rrrr"szDictConfigurator.configure_rootN)F) rrrrrrrrrrrrrrrrrrs$ ?   rcCst|dS)z%Configure logging using a dictionary.N)dictConfigClassrrrrr dictConfig)srcsDGdddt}Gdddt}Gfdddtj||||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@r)z#listen..ConfigStreamHandlerz Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c Ss\z|j}|d}t|dkrtd|d}|j|}t||kr4|||t|}t||ks#|jjdur@|j|}|dur|d}zddl}| |}t |t sZJt |Wn"t yt|}zt|Wn t y~tYnwYnw|jjr|jjWdSWdSWdSty}z |jtkrWYd}~dSd}~ww)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) connectionrecvr9structunpackserververifydecodejsonloadsrrrrr StringIOr traceback print_excreadysetOSErrorerrno RESET_ERROR)rconnchunkslenr rfilerrrrhandleIsJ               z*listen..ConfigStreamHandler.handleN)rrrrrrrrrConfigStreamHandlerBs rc@s0eZdZdZdZdedddfddZddZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rm localhostNcSs>t|||f|td|_td|_||_||_dS)Nrrm) rrrrabortrtimeoutrr )rhostportrrr rrrrws z-listen..ConfigSocketReceiver.__init__cSsbddl}d}|s+||jggg|j\}}}|r|t|j}t|r| dS)Nr) selectsocketfilenorhandle_requestrrrr server_close)rr!rrdwrexrrrserve_until_stoppeds  z8listen..ConfigSocketReceiver.serve_until_stopped)rrrrallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTrr)rrrrConfigSocketReceiverps r,cs&eZdZfddZddZZS)zlisten..Servercs4t|||_||_||_||_t|_dSr*) superrrcvrhdlrr r  threadingEventr)rr.r/r r )Server __class__rrrs zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr|jd|_|jt|a t | dS)N)r rrr rrm) r.r r/rr server_addressrrr _listenerrr))rr rrrruns     zlisten..Server.run)rrrrr6 __classcell__rr2)r3rr2sr2)rrr0Thread)r r rr,rr8rlisten.s.r:cCs>tztrdt_daWtdSWtdStw)zN Stop the listening server which was created with a call to listen(). rmN)rrr5rrrrrr stopListenings r;)NTN)+rrr rlogging.handlersrrrr0r socketserverrrr+ ECONNRESETrr5rr)r/rrrirrrIrrobjectrrrrnrrrrrrrr:r;rrrrsJ  #&W! B z