U ,a@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|}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). rNreadline) configparser isinstanceZRawConfigParserZ ConfigParserhasattrZ read_fileread_create_formatterslogging _acquireLock _releaseLock_clearExistingHandlers_install_handlers_install_loggers)Zfnamedefaultsdisable_existing_loggersrcp formattershandlersr3/opt/alt/python38/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)splitpop __import__getattrAttributeError)nameusedfoundnrrr_resolveUs   r"cCs ttj|SN)mapstrstrip)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 } | rt| }||||} | ||<q2|S) zCreate and return formattersrkeys,z formatter_%sformatTN)rawfallbackdatefmtstyle%class)lenrr'getr Formatterr") rZflistrZformZsectnameZfsZdfsZstlc class_namefrrrr fs$    r c Cs^|dd}t|siS|d}t|}i}g}|D]}|d|}|d}|dd}zt|tt}Wn ttfk rt |}YnX|dd } t| tt} |d d } t| tt} || | } d |kr|d } | | t|r| ||t |tj jr2|d d} t| r2|| | f| ||<q6|D]\} }| ||q@|S)zInstall and return handlersrr(r)z handler_%sr0 formatterargs()kwargsz{}leveltarget)r1rr'r2evalvarsr r NameErrorr"setLevel setFormatter issubclassr MemoryHandlerappendZ setTarget)rrhlistrZfixupshandsectionklassfmtr9r;hr<r=trrrr|sB             rcCsTtj}|D]D}|jj|}||krHt|tjsN|tjg|_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 rootmanager loggerDictrZ PlaceHolderrAZNOTSETr propagatedisabled)existing child_loggersdisable_existingrMlogloggerrrr_handle_existing_loggerss    rWcCs |dd}|d}tt|}|d|d}tj}|}d|krX|d}|||jddD]}||qf|d} t | r| d} t| } | D]} | || qt|j j } | g} |D](}|d |}|d } |jd d d }t| }| | krv| | d }| d}t |}t | }||krl| |d||kr`| | ||d 7}q2| | d|kr|d}|||jddD]}||q||_d|_|d} t | r| d} t| } | D]} | || qqt| | |dS)zCreate and install loggersloggersr(r)rMZ logger_rootr<Nrz logger_%squalnamerP)r,rr)rlistr'remover rMrAr removeHandlerr1 addHandlerrNrOr(sortZgetint getLoggerindexrErPrQrW)rrrTZllistrHrMrUr<rKrFrGrRrSZqnrPrViprefixedpflen num_existingrrrrsd                  rcCs.tjttjddtjdd=dS)z!Clear and close existing handlersN)r _handlersclearZshutdownZ _handlerListrrrrr s r z^[a-z_][a-z0-9_]*$cCst|}|std|dS)Nz!Not a valid Python identifier: %rT) IDENTIFIERmatch ValueError)smrrr valid_idents  rmc@s"eZdZdZdddZddZdS) ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j|}||k r>|r |||<t|tttfkr>||_||_|Sr#) configuratorconverttypeConvertingDictConvertingListConvertingTupleparentkey)selfrvvaluereplaceresultrrrconvert_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,eZdZdZddZd ddZd ddZdS) rrz A converting dictionary wrapper.cCst||}|||Sr#)dict __getitem__r{rwrvrxrrrrCs zConvertingDict.__getitem__NcCst|||}|||Sr#)rr2r{rwrvdefaultrxrrrr2GszConvertingDict.getcCst|||}|j||ddSNF)ry)rrr{rrrrrKszConvertingDict.pop)N)N)r|r}r~rrr2rrrrrrr@s rrc@s"eZdZdZddZdddZdS) rszA converting list wrapper.cCst||}|||Sr#)r[rr{rrrrrQs zConvertingList.__getitem__cCst||}||Sr#)r[rrp)rwidxrxrrrrUs zConvertingList.popN)r)r|r}r~rrrrrrrrsOsrsc@seZdZdZddZdS)rtzA converting tuple wrapper.cCst||}|j||ddSr)tuplerr{rrrrr[s zConvertingTuple.__getitem__N)r|r}r~rrrrrrrtYsrtc@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)ZextZcfgcCst||_||j_dSr#)rrconfigro)rwrrrr__init__ts zBaseConfigurator.__init__c Cs|d}|d}z^||}|D]H}|d|7}zt||}Wq$tk rj||t||}Yq$Xq$|WStk rtdd\}}td||f}|||_ |_ |YnXdS)z` Resolve strings to objects using standard import and attribute syntax. rrrZNzCannot resolve %r: %s) rrimporterrr ImportErrorsysexc_inforj __cause__ __traceback__) rwrkrrr Zfragetbvrrrresolvexs"     zBaseConfigurator.resolvecCs ||S)z*Default converter for the ext:// protocol.)rrwrxrrrrszBaseConfigurator.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||fqH|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) WORD_PATTERNrirjendrgroups DOT_PATTERN INDEX_PATTERN DIGIT_PATTERNint TypeError)rwrxrestrldrr!rrrrs4       zBaseConfigurator.cfg_convertcCst|ts$t|tr$t|}||_nt|tsHt|trHt|}||_nt|tsvt|trvt|dsvt|}||_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. _fieldsprefixNsuffix)rrrrrorsr[rtrrr%CONVERT_PATTERNri groupdictvalue_convertersr2r)rwrxrlrrZ converterrrrrrps0    zBaseConfigurator.convertcsjd}t|s||}dd}fddD}|f|}|rf|D]\}}t|||qP|S)z1Configure an object with a user-supplied factory.r:rNcsi|]}t|r||qSrrm.0krrr sz5BaseConfigurator.configure_custom..)rcallableritemssetattr)rwrr4propsr;rzrrxrrrconfigure_customs    z!BaseConfigurator.configure_customcCst|trt|}|S)z0Utility function which converts lists to tuples.)rr[rrrrras_tuples zBaseConfigurator.as_tupleN)r|r}r~rrecompilerrrrrr staticmethodrrrrrrrprrrrrrr`s"     "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|krtd|ddkr2td|d|dd}i}tzn|r|d|}|D]}|tjkrtd|qdz6tj|}||}|d d }|r|t |Wqdt k r} ztd || W5d } ~ XYqdXqd|d |} | D]N}z| || |d Wqt k rF} ztd|| W5d } ~ XYqXq|dd } | rz| | 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| jkr||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 } ~ XYnXqt||| |dd } | rz| | Wn.t k r} ztd| W5d } ~ XYnXW5tXd 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 %rfilterszUnable to configure filter %rtarget not configured yetr) rrjrr r r r2rfrA _checkLevel Exceptionconfigure_loggerconfigure_rootr configure_formatterconfigure_filtersortedconfigure_handlerrr%rrErMr[rNrOr(r_rar1r\rW)rwrrZ EMPTY_DICTrrhandlerZhandler_configr<rrXrMrTrrZdeferredrRrSrbrcrdrerrr configures                        zDictConfigurator.configurec Csd|krr|d}z||}Wqtk rn}z2dt|kr>|d|d<||d<||}W5d}~XYqXnl|dd}|dd}|dd}|d d}|stj} nt|} d |kr| ||||d }n | |||}|S) z(Configure a formatter from a dictionary.r:z'format'r*rJNr-r.r/r0Zvalidate)rrr%rr2r r3r") rwrfactoryrzterJZdfmtr.cnamer4rrrrs*      z$DictConfigurator.configure_formattercCs.d|kr||}n|dd}t|}|S)z%Configure a filter from a dictionary.r:rr8)rr2r ZFilter)rwrrzrrrrrs    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.rzUnable to add filter %rN)Z addFilterrrrj)rwZfiltererrr6rrrr add_filterss zDictConfigurator.add_filtersc st}dd}|r\z|jd|}Wn0tk rZ}ztd||W5d}~XYnXdd}dd}dkrd}t|s||}|}nd} || } t| tj j rFd krFz>|jd d } t | tj s |td | d <Wn6tk rB}ztd d |W5d}~XYnXnZt| tj jrtd krt|d d <n,t| tj jrdkr|dd<| }dd} fddD} 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<rr:r0r=rrzUnable to set target handler %rZmailhostZaddressrcsi|]}t|r||qSrrrrrrrsz6DictConfigurator.configure_handler..z'stream'streamZstrm)rrrrrjrrrCr rrDrZHandlerupdaterZ SMTPHandlerrZ SysLogHandlerr%rBrArrrr)rwrZ config_copyr7rr<rr4rrrIZthrr;rzrrrxrrrrs~             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^rrrj)rwrVrrKrrrr add_handlerss zDictConfigurator.add_handlersFcCs|dd}|dk r$|t||s~|jddD]}||q6|dd}|rb||||dd}|r~|||dS)zU Perform configuration which is common to root and non-root loggers. r<Nrr)r2rAr rrr]rr)rwrVrrr<rKrrrrrcommon_logger_configs     z%DictConfigurator.common_logger_configcCs6t|}|||||dd}|dk r2||_dS)z.Configure a non-root logger from a dictionary.rPN)r r`rr2rP)rwrrrrVrPrrrrs   z!DictConfigurator.configure_loggercCst}||||dS)z*Configure a root logger from a dictionary.N)r r`r)rwrrrMrrrrszDictConfigurator.configure_rootN)F)F)F) r|r}r~rrrrrrrrrrrrrrrs$ ?  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@seZdZdZddZdS)z#listen..ConfigStreamHandlerz Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c SsDz |j}|d}t|dkr td|d}|j|}t||krb|||t|}q>|jjdk rz|j|}|dk r|d}zddl}| |}t |WnHt k rt |}z t|Wnt k rtYnXYnX|jjr |jjWn2tk r>}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 connectionZrecvr1structZunpackserververifydecodejsonloadsrrioStringIOr traceback print_excreadysetOSErrorerrno RESET_ERROR)rwZconnchunkZslenrrfilerrrrhandleFs6            z*listen..ConfigStreamHandler.handleN)r|r}r~rrrrrrConfigStreamHandler?src@s0eZdZdZdZdedddfddZddZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rZZ localhostNcSs>t|||f|td|_td|_||_||_dS)NrrZ) rrr r abortr timeoutrr)rwZhostportrrrrrrrtsz-listen..ConfigSocketReceiver.__init__cSs`ddl}d}|sT||jggg|j\}}}|r<|t|j}tq | dS)Nr) selectZsocketfilenorZhandle_requestr r rr Z server_close)rwrrZrdZwrZexrrrserve_until_stopped~s  z8listen..ConfigSocketReceiver.serve_until_stopped)r|r}r~rZallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTrrrrrrConfigSocketReceiverms rcs&eZdZfddZddZZS)zlisten..Servercs4t|||_||_||_||_t|_dSr#) superrrcvrhdlrrr threadingZEventr)rwrrrr)Server __class__rrrs zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr0|jd|_|jt|a t | dS)N)rrrrrrZ) rrrrrZserver_addressrr r _listenerr r)rwrrrrruns    zlisten..Server.run)r|r}r~rr __classcell__rr)rrrsr)rrrZThread)rrrrrrrlisten+s.rcCs*tztrdt_daW5tXdS)zN Stop the listening server which was created with a call to listen(). rZN)r r r rrrrrr stopListenings r)NT)+rrrr Zlogging.handlersrrrrrZ socketserverrrrZ ECONNRESETrrrr"r'r rrWrr rIrhrmobjectrnrrrr[rsrrtrrrrrrrrrrsH  "%W! Az