Fah @sdZddlZddlZddlZddlZddlZddlZddlZddlZyddl Z ddl Z Wne k rdZ YnXddl mZmZdZejZdaddddZdd Zd d Zd d ZddZddZddZejdejZddZGdddeZ Gddde!e Z"Gddde#e Z$Gddde%e Z&Gdd d eZ'Gd!d"d"e'Z(e(Z)d#d$Z*edd%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-2014 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! N)ThreadingTCPServerStreamRequestHandleriF#Tc Csddl}t||jr'|}n;|j|}t|drU|j|n |j|t|}tj z@tj j tj dd=t ||}t|||WdtjXdS)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 _handlersclear _handlerList_install_handlers_install_loggers _releaseLock)ZfnameZdefaultsdisable_existing_loggersrcp formattershandlersr$/usr/lib/python3.5/logging/config.py fileConfig8s      rc Cs|jd}|jd}t|}x]|D]U}|d|}yt||}Wq1tk rt|t||}Yq1Xq1W|S)z)Resolve a dotted name to a global object..r)splitpop __import__getattrAttributeError)nameusedfoundnrrr_resolveZs    r#cCstdd|S)NcSs |jS)N)strip)xrrrisz_strip_spaces..)map)Zalistrrr _strip_spaceshsr(c Cs|dd}t|siS|jd}t|}i}x|D]}d|}|j|ddddd }|j|d dddd }|j|d dddd }tj}||jd } | rt| }||||} | ||           rcCsdtj}xT|D]L}|jj|}||krStj|_g|_d|_q||_qWdS)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 loggerDictZNOTSETr:r propagatedisabled)existing child_loggersdisable_existingrKlogloggerrrr_handle_existing_loggerss      rUcCs|dd}|jd}ttdd|}|jd|d}tj}|}d|kr|d}|j|x(|jd d D]}|j|qW|d } t | r| jd} t | } x| D]} |j || qWt|j j j} | jg} x|D]}|d |}|d } |jd dd}tj| }| | kr| j| d}| d}t |}t | }xE||kr| |d ||kr| j| ||d7}qW| j| d|kr)|d}|j|x(|jd d D]}|j|q=W||_d|_|d } t | r.| jd} t | } x| D]} |j || qWq.Wt| | |d S)zCreate and install loggersloggersr)r*cSs |jS)N)r$)r%rrrr&sz"_install_loggers..rKZ logger_rootr:Nrz logger_%sZqualnamerNr-rr)rlistr'remover rKr?r removeHandlerr2r( addHandlerrLrMr)sortZgetint getLoggerindexrCrNrOrU)rrrRZllistrFrKrSr:rIrDrErPrQZqnrNrTiprefixedpflen num_existingrrrrsd                            rz^[a-z_][a-z0-9_]*$cCs)tj|}|s%td|dS)Nz!Not a valid Python identifier: %rT) IDENTIFIERmatch ValueError)smrrr valid_identsrhc@s1eZdZdZdddZddZdS)ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCs_|jj|}||k r[|r.|||[a-z]+)://(?P.*)$z ^\s*(\w+)\s*z^\.\s*(\w+)\s*z^\[\s*(\w+)\s*\]\s*z^\d+$Zext ext_convertZcfg cfg_convertcCst||_||j_dS)N)rmconfigrj)rrrrrr__init__nszBaseConfigurator.__init__c Cs|jd}|jd}yz|j|}x`|D]X}|d|7}yt||}Wq7tk r|j|t||}Yq7Xq7W|SWn]tk rtjdd\}}td||f}|||_ |_ |YnXdS)z` Resolve strings to objects using standard import and attribute syntax. rrrWNzCannot resolve %r: %s) rrimporterrr ImportErrorsysexc_infore __cause__ __traceback__) rrrfrr r!Zfragetbvrrrresolvers"    zBaseConfigurator.resolvecCs |j|S)z*Default converter for the ext:// protocol.)r)rrrsrrrrszBaseConfigurator.ext_convertc CsY|}|jj|}|dkr7td|n||jd}|j|jd}x|rT|jj|}|r||jd}n|jj|}|r|jd}|jj|s||}n9yt |}||}Wnt k r||}YnX|r;||jd}qgtd||fqgW|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) WORD_PATTERNrdreendrgroups DOT_PATTERN INDEX_PATTERN DIGIT_PATTERNint TypeError)rrrsrestrgdr~r"rrrrs2     zBaseConfigurator.cfg_convertcCs&t|t r7t|tr7t|}||_nt|t rnt|trnt|}||_nt|t rt|trt|}||_n}t|tr"|j j |}|r"|j }|d}|j 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. prefixNsuffix)rrmr{rjrnrXrorstrCONVERT_PATTERNrd groupdictvalue_convertersr3r)rrrsrgrrZ converterrrrrrks*          zBaseConfigurator.convertcsjd}t|s*|j|}jdd}tfddD}||}|rx*|jD]\}}t|||qzW|S)z1Configure an object with a user-supplied factory.z()rNcs,g|]"}t|r||fqSr)rh).0k)rrr s z5BaseConfigurator.configure_custom..)rcallablerr{itemssetattr)rrrr5propskwargsrurrsr)rrconfigure_customs  z!BaseConfigurator.configure_customcCst|trt|}|S)z0Utility function which converts lists to tuples.)rrXr)rrrsrrras_tuples zBaseConfigurator.as_tupleN)rwrxryrzrecompilerrrrrr staticmethodrrrrrrrkrrrrrrrZs       "  rc@seZdZdZddZddZddZdd Zd d Zd d Z dddZ dddZ dddZ dS)DictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. cCs<|j}d|kr!td|ddkrEtd|d|jdd}i}tjz|r|jd|}x|D]}|tjkrtd|qyItj|}||}|jd d }|r|jtj|Wqt k r7} ztd || fWYd d } ~ XqXqW|jd |} xg| D]_}y|j || |d WqUt k r} ztd|| fWYd d } ~ XqUXqUW|jdd } | r)y|j | d Wq)t k r} ztd| WYd d } ~ Xq)Xn |jdd } tjj tj d d =|jd|} xg| D]_}y|j| || |.z'stream'streamZstrm)r{rrrrerrrAr rrBrZHandlerupdaterZ SMTPHandlerrZ SysLogHandlerrr@r?rrrr)rrrZ config_copyr7rr:rr5rrrGZthrrrurrrsr)rrrsl #      *    z"DictConfigurator.configure_handlercCsoxh|D]`}y|j|jd|Wqtk rf}ztd||fWYdd}~XqXqWdS)z.Add handlers to a logger from a list of names.rzUnable to add handler %r: %sN)r[rrre)rrrTrrIrrrr add_handlerss  zDictConfigurator.add_handlersFcCs|jdd}|dk r4|jtj||sx(|jddD]}|j|qNW|jdd}|r|j|||jdd}|r|j||dS)zU Perform configuration which is common to root and non-root loggers. r:Nrr)r3r?r rrrZrr)rrrTrrr:rIrrrrrcommon_logger_configs z%DictConfigurator.common_logger_configcCsMtj|}|j||||jdd}|dk rI||_dS)z.Configure a non-root logger from a dictionary.rNN)r r]rr3rN)rrrrrrTrNrrrr s  z!DictConfigurator.configure_loggercCs#tj}|j|||dS)z*Configure a root logger from a dictionary.N)r r]r)rrrrrKrrrrs zDictConfigurator.configure_rootN) rwrxryrzrrrrrrrrrrrrrrs    ? rcCst|jdS)z%Configure logging using a dictionary.N)dictConfigClassr)rrrr dictConfigsrcsptstdGdddt}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. z listen() needs threading to workc@s"eZdZdZddZdS)z#listen..ConfigStreamHandlerz Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. cSsyv|j}|jd}t|dkrutjd|d}|jj|}x3t||kr||j|t|}qXW|jjdk r|jj|}|dk rY|jd}y>ddl}|j |}t |t st t |WnNtk rXtj|}yt|Wntk rStjYnXYnX|jjru|jjjWn7tk r}z|jtkrWYdd}~XnXdS)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 connectionZrecvr2structZunpackserververifydecodejsonloadsrr{AssertionErrorrrioStringIOr traceback print_excreadysetOSErrorerrno RESET_ERROR)rrZconnchunkZslenrrfilerrrrhandle;s8 !     z*listen..ConfigStreamHandler.handleN)rwrxryrzrrrrrConfigStreamHandler4s rc@sCeZdZdZdZdedddddZddZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rWZ localhostNcSsUtj|||f|tjd|_tjd|_||_||_dS)NrrW) rrr r abortrtimeoutrr)rrZhostportrrrrrrris     z-listen..ConfigSocketReceiver.__init__cSsddl}d}xg|s{|j|jjggg|j\}}}|r[|jtj|j}tjqW|jj dS)Nr) selectZsocketfilenorZhandle_requestr r rrclose)rrrrZrdwrZexrrrserve_until_stoppedss     z8listen..ConfigSocketReceiver.serve_until_stopped)rwrxryrzZallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTrrrrrrConfigSocketReceiverbs  rcs1eZdZfddZddZS)zlisten..ServercsJt|j||_||_||_||_tj|_dS)N) superrrcvrhdlrrr threadingZEventr)rrrrrr)Server __class__rrrs     zlisten..Server.__init__c Ss|jd|jd|jd|jd|j}|jdkrO|jd|_|jjtj|a tj |j dS)NrrrrrrW) rrrrrZserver_addressrr r _listenerrr)rrrrrrruns     zlisten..Server.run)rwrxryrrr)r)rrrs r)threadNotImplementedErrorrrrZThread)rrrrr)rrlistens  .rc Cs5tjztr"dt_daWdtjXdS)zN Stop the listening server which was created with a call to listen(). rWN)r r rrrrrrr stopListenings    r)-rzrrr Zlogging.handlersrrrr_threadrrrZ socketserverrrrZ ECONNRESETrrrr#r(r rrUrrIrcrhobjectrir{rmrXrnrrorrrrrrrrrrsJ            "    #  V ! 9 |