j f @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 formattershandlersr3/opt/alt/python34/lib64/python3.4/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 }tj}||jd }|rt|}n|||} | ||          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 loggerDictZNOTSETr8r propagatedisabled)existing child_loggersdisable_existingrIlogloggerrrr_handle_existing_loggerss      rScCs|dd}|jd}ttdd|}|jd|d}tj}|}d|kr|d}|j|nx(|jd d D]}|j|qW|d } t | r| jd} t | } x"| D]} |j || qWnt|j j j} | jg} x|D]}|d |}|d } |jd dd}tj| }| | kr| j| d}| d}t |}t | }xH||kr| |d ||kr| j| |n|d7}qW| j| nd|kr8|d}|j|nx(|jd d D]}|j|qLW||_d|_|d } t | r4| jd} t | } x"| D]} |j || qWq4q4Wt| | |d S)zCreate and install loggersloggersr)r*cSs |jS)N)r$)r%rrrr&sz"_install_loggers..rIZ logger_rootr8Nrz logger_%sZqualnamerLr-rr)rlistr'remover rIr=r removeHandlerr0r( addHandlerrJrKr)sortZgetint getLoggerindexrArLrMrS)rrrPZllistrDrIrQr8rGrBrCrNrOZqnrLrRiprefixedpflen num_existingrrrrsd                         rz^[a-z_][a-z0-9_]*$cCs,tj|}|s(td|ndS)Nz!Not a valid Python identifier: %rT) IDENTIFIERmatch ValueError)smrrr valid_identsrfc@s1eZdZdZdddZddZdS)ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsh|jj|}||k rd|r1|||[a-z]+)://(?P.*)$z ^\s*(\w+)\s*z^\.\s*(\w+)\s*z^\[\s*(\w+)\s*\]\s*z^\d+$ ext_convertZext cfg_convertZcfgcCst||_||j_dS)N)rkconfigrh)rprrrr__init__mszBaseConfigurator.__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. rrrUNzCannot resolve %r: %s) rrimporterrr ImportErrorsysexc_inforc __cause__ __traceback__) rprdrr r!Zfragetbvrrrresolveqs"    zBaseConfigurator.resolvecCs |j|S)z*Default converter for the ext:// protocol.)r)rprqrrrrszBaseConfigurator.ext_convertc Cs\|}|jj|}|dkr7td|n!||jd}|j|jd}x|rW|jj|}|r||jd}n|jj|}|r|jd}|jj|s||}qyt |}||}Wqt k r||}YqXn|r>||jd}qgtd||fqgW|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) WORD_PATTERNrbrcendrgroups DOT_PATTERN INDEX_PATTERN DIGIT_PATTERNint TypeError)rprqrestredr|r"rrrrs2     zBaseConfigurator.cfg_convertcCs/t|t r7t|tr7t|}||_nt|t rnt|trnt|}||_nt|t rt|trt|}||_nt|tr+|j j |}|r+|j }|d}|j j |d}|r(|d}t||}||}q(q+n|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)rrkryrhrlrVrmr~strCONVERT_PATTERNrb groupdictvalue_convertersr1r)rprqrerrZ converterrrrrris*         zBaseConfigurator.convertcsjd}t|s-|j|}njdd}tfddD}||}|rx-|jD]\}}t|||q}Wn|S)z1Configure an object with a user-supplied factory.z()rNcs,g|]"}t|r||fqSr)rf).0k)rrr s z5BaseConfigurator.configure_custom..)rcallablerryitemssetattr)rprr3propskwargsrsrrqr)rrconfigure_customs  z!BaseConfigurator.configure_customcCs"t|trt|}n|S)z0Utility function which converts lists to tuples.)rrVr~)rprqrrras_tupleszBaseConfigurator.as_tupleN)rurvrwrxrecompilerrrrrr staticmethodrrrrrrrirrrrrrrYs"       "  rc@seZdZdZddZddZddZdd Zd d Zd d Z dddZ dddZ dddZ dS)DictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. cCsQ|j}d|kr$tdn|ddkrKtd|dn|jdd}i}tjz|r+|jd|}x|D]}|tjkrtd|qyLtj|}||}|jd d }|r|jtj|nWqt k r@} ztd || fWYd d } ~ XqXqW|jd |} xg| D]_}y|j || |d Wq^t k r} ztd|| fWYd d } ~ Xq^Xq^W|jdd } | r>y|j | d Wq(t k r$} ztd| WYd d } ~ Xq(Xq>n|jdd } tjj tj d d =|jd|} xg| D]_}y|j| || |y|j | Wq>t k r:} ztd| WYd d } ~ Xq>XnWd tjXd S)zDo the configuration.versionz$dictionary doesn't specify a versionrUzUnsupported version: %s incrementalFrzNo handler found with name %rr8Nz"Unable to configure handler %r: %srTTz!Unable to configure logger %r: %srIz#Unable to configure root logger: %srrz$Unable to configure formatter %r: %sfiltersz!Unable to configure filter %r: %sztarget not configured yetr) rrcrr r r1r r= _checkLevel Exceptionconfigure_loggerconfigure_rootr rconfigure_formatterconfigure_filtersortedconfigure_handlerrrrArIrVrJrKr)rZr\r0rWrSr)rprrZ EMPTY_DICTrrhandlerZhandler_configr8rrTrIrPrrZdeferredrNrOr]r^r_r`rrr configures       ' '#  ' ' '  '       ' $zDictConfigurator.configurecCsd|kr|d}y|j|}Wqtk r}zIdt|krVn|jd|d<||d<|j|}WYdd}~XqXnK|jdd}|jdd}|jdd}tj|||}|S) z(Configure a formatter from a dictionary.z()z'format'r+rFNr.style%)rrrrr1r r2)rprfactoryrsterFZdfmtrrrrrs   %z$DictConfigurator.configure_formattercCsCd|kr|j|}n!|jdd}tj|}|S)z%Configure a filter from a dictionary.z()rr6)rr1r ZFilter)rprrsrrrrrs  z!DictConfigurator.configure_filtercCsoxh|D]`}y|j|jd|Wqtk rf}ztd||fWYdd}~XqXqWdS)z/Add filters to a filterer from a list of names.rzUnable to add filter %r: %sN)Z addFilterrrrc)rpZfiltererrr4rrrr add_filterss  zDictConfigurator.add_filtersc/s_t}jdd}|rzy|jd|}Wqztk rv}ztd||fWYdd}~XqzXnjdd}jdd}dkrjd}t|s|j|}n|}nQjd} |j| } t| tj j rd kryQ|jd d } t | tj sej |td n| d .z'stream'streamZstrm)ryrrrrcrrr?r rr@rZHandlerupdaterZ SMTPHandlerrZ SysLogHandlerrr>r=rrrr)rprZ config_copyr5rr8rr3rZcnamerEZthrrrsrrrqr)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)rYrrrc)rprRrrGrrrr add_handlerss  zDictConfigurator.add_handlersFcCs|jdd}|dk r7|jtj|n|sx(|jddD]}|j|qQW|jdd}|r|j||n|jdd}|r|j||qndS)zU Perform configuration which is common to root and non-root loggers. r8Nrr)r1r=r rrrXrr)rprRrrr8rGrrrrrcommon_logger_configs z%DictConfigurator.common_logger_configcCsPtj|}|j||||jdd}|dk rL||_ndS)z.Configure a non-root logger from a dictionary.rLN)r r[rr1rL)rprrrrRrLrrrrs  z!DictConfigurator.configure_loggercCs#tj}|j|||dS)z*Configure a root logger from a dictionary.N)r r[r)rprrrIrrrr s zDictConfigurator.configure_rootN) rurvrwrxrrrrrrrrrrrrrrs    ? rcCst|jdS)z%Configure logging using a dictionary.N)dictConfigClassr)rrrr dictConfigsrcsststdnGdddt}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. cSsy|j}|jd}t|dkrtjd|d}|jj|}x3t||kr||j|t|}qXW|jjdk r|jj|}n|dk r_|jd}y>ddl}|j |}t |t st t |Wq_tk r[tj|}yt|Wntk rVtjYnXYq_Xn|jjr|jjjqnWn:tk r}z|jtkrnWYdd}~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 connectionZrecvr0structZunpackserververifydecodejsonloadsrryAssertionErrorrrioStringIOr traceback print_excreadysetOSErrorerrno RESET_ERROR)rpZconnchunkZslenrrfilerrrrhandle5s8 !     z*listen..ConfigStreamHandler.handleN)rurvrwrxrrrrrConfigStreamHandler.s rc@sCeZdZdZdZdedddddZddZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rUZ localhostNcSsUtj|||f|tjd|_tjd|_||_||_dS)NrrU) rrr r abortrtimeoutrr)rphostportrrrrrrrcs     z-listen..ConfigSocketReceiver.__init__cSsddl}d}xj|s~|j|jjggg|j\}}}|r^|jntj|j}tjqW|jj dS)Nr) selectZsocketfilenorZhandle_requestr r rrclose)rprrZrdwrZexrrrserve_until_stoppedms     z8listen..ConfigSocketReceiver.serve_until_stopped)rurvrwrxZallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTrrrrrrConfigSocketReceiver\s  rcs1eZdZfddZddZS)zlisten..ServercsJt|j||_||_||_||_tj|_dS)N) superrrcvrhdlrrr threadingZEventr)rprrrr)Server __class__rrr}s     zlisten..Server.__init__c Ss|jd|jd|jd|jd|j}|jdkrR|jd|_n|jjtj|a tj |j dS)NrrrrrrU) rrrrrZserver_addressrr r _listenerrr)rprrrrruns     zlisten..Server.run)rurvrwrrr)r)rrr{s r)threadNotImplementedErrorrrrZThread)rrrrr)rrlistens .rc Cs8tjztr%dt_danWdtjXdS)zN Stop the listening server which was created with a call to listen(). rUN)r r rrrrrrr stopListenings    r)-rxrrr Zlogging.handlersrrrr_threadrrrZ socketserverrrrZ ECONNRESETrrrr#r(r rrSrrIrarfobjectrgryrkrVrlr~rmrrrrrrrrrrsJ            "    #  V ! 4 |