3 \ @sddlZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl Z y ddl Z Wne k rxdZ YnXdZdZdZdZdZdZd'ZGd d d ejZGd d d eZGdddeZGdddejZGdddejZGdddeZGdddejZGdddejZGdddejZGdddejZ GdddejZ!Gd d!d!e!Z"Gd"d#d#ejZ#e rGd$d%d%e$Z%dS)(N)ST_DEVST_INOST_MTIMEi<#i=#i>#i?#i<c@s.eZdZd ddZddZddZd d ZdS) BaseRotatingHandlerNFcCs0tjj|||||||_||_d|_d|_dS)N)logging FileHandler__init__modeencodingnamerrotator)selffilenamer r delayr(/usr/lib64/python3.6/logging/handlers.pyr 5s zBaseRotatingHandler.__init__c CsHy$|j|r|jtjj||Wntk rB|j|YnXdS)N)shouldRollover doRolloverrr emit Exception handleError)rrecordrrrr?s  zBaseRotatingHandler.emitcCst|js|}n |j|}|S)N)callabler )rZ default_nameresultrrrrotation_filenameMs  z%BaseRotatingHandler.rotation_filenamecCs4t|js$tjj|r0tj||n |j||dS)N)rrospathexistsrename)rsourcedestrrrrotate`s  zBaseRotatingHandler.rotate)NF)__name__ __module__ __qualname__r rrr#rrrrr/s rc@s&eZdZd ddZddZd d ZdS) RotatingFileHandlerarNFcCs.|dkr d}tj|||||||_||_dS)Nrr()rr maxBytes backupCount)rrr r)r*r rrrrr zs zRotatingFileHandler.__init__cCs|jr|jjd|_|jdkrxtt|jdddD]^}|jd|j|f}|jd|j|df}tjj|r4tjj|rtj |tj ||q4W|j|jd}tjj|rtj ||j |j||j s|j |_dS)Nrz%s.%dz.1)streamcloser*ranger baseFilenamerrrremover r#r_open)riZsfndfnrrrrs$        zRotatingFileHandler.doRollovercCsZ|jdkr|j|_|jdkrVd|j|}|jjdd|jjt||jkrVdSdS)Nrz%s r+)r-r2r)formatseektelllen)rrmsgrrrrs   z"RotatingFileHandler.shouldRollover)r(rrNF)r$r%r&r rrrrrrr'us r'c@s6eZdZdddZdd Zd d Zd d ZddZdS)TimedRotatingFileHandlerhr+rNFc Cstj||d|||j|_||_||_||_|jdkrNd|_d|_d|_ n|jdkrld|_d|_d |_ n|jd krd|_d |_d |_ n|jd ks|jdkrd|_d|_d|_ n|jj dr.d|_t |jdkrt d|j|jddks|jddkrt d|jt |jd|_d|_d|_ nt d|jtj|j tj|_ |j||_|j}tjj|r~tj|t} n t tj} |j| |_dS) Nr(Sr+z%Y-%m-%d_%H-%M-%Sz-^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$Mrz%Y-%m-%d_%H-%Mz'^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}(\.\w+)?$Hz %Y-%m-%d_%Hz!^\d{4}-\d{2}-\d{2}_\d{2}(\.\w+)?$DMIDNIGHTrz%Y-%m-%dz^\d{4}-\d{2}-\d{2}(\.\w+)?$Wr5zHYou must specify a day for weekly rollover from 0 to 6 (0 is Monday): %s06z-Invalid day specified for weekly rollover: %sz'Invalid rollover interval specified: %siiiQiiQi: )rr upperwhenr*utcatTimeintervalsuffixextMatch startswithr9 ValueErrorint dayOfWeekrecompileASCIIr0rrrstatrtimecomputeRollover rolloverAt) rrrGrJr*r rrHrItrrrr sL        z!TimedRotatingFileHandler.__init__cCsd||j}|jdks"|jjdr`|jr4tj|}n tj|}|d}|d}|d}|d}|jdkrnt}n |jj d|jj d|jj }||d|d|} | dkr| t7} |d d }|| }|jjdr`|} | |j kr`| |j kr|j | } nd| |j d } || d} |js\|d} tj| d}| |kr\| sPd}nd }| |7} | }|S)NrArBrrr+rCriiiQr,r,i) rJrGrMrHrUgmtime localtimerI _MIDNIGHTZhourZminutesecondrP)r currentTimerrXZ currentHourZ currentMinuteZ currentSecondZ currentDayZ rotate_tsrZdayZ daysToWait newRolloverAtdstNow dstAtRolloveraddendrrrrVsH           z(TimedRotatingFileHandler.computeRollovercCsttj}||jkrdSdS)Nr+r)rOrUrW)rrrXrrrrKs  z'TimedRotatingFileHandler.shouldRolloverc Cstjj|j\}}tj|}g}|d}t|}xH|D]@}|d||kr6||d}|jj|r6|jtjj ||q6Wt||j krg}n|j |dt||j }|S)N.) rrsplitr0listdirr9rLmatchappendjoinr*sort) rZdirNameZbaseNameZ fileNamesrprefixZplenZfileNamerKrrrgetFilesToDeleteWs    z)TimedRotatingFileHandler.getFilesToDeletec Cs|jr|jjd|_ttj}tj|d}|j|j}|jrNtj|}n6tj|}|d}||kr|rrd}nd }tj||}|j |j dtj |j |}t jj|rt j||j|j ||jdkrx|jD]}t j|qW|js|j|_|j|} x| |kr"| |j} q W|jdks>|jjdrx|j rxtj| d } || krx|sld }nd}| |7} | |_dS) Nr+irgrrArBr,r,ir,i)r-r.rOrUr^rWrJrHr]rr0ZstrftimerKrrrr1r#r*rorr2rVrGrM) rrardrXZ timeTupleZdstThenrfr4srcrerrrrnsH            $ z#TimedRotatingFileHandler.doRollover)r<r+rNFFN)r$r%r&r rVrrorrrrrr;s  9I r;c@s.eZdZd ddZddZdd Zd d ZdS) WatchedFileHandlerr(NFcCs,tjj|||||d\|_|_|jdS)Nr+r,r,)r,r,)rr r devino _statstream)rrr r rrrrr s zWatchedFileHandler.__init__cCs0|jr,tj|jj}|t|t|_|_dS)N)r-rfstatfilenorrrrrs)rsresrrrrtszWatchedFileHandler._statstreamc Csytj|j}Wntk r(d}YnX| sL|t|jksL|t|jkr|jdk r|jj |jj d|_|j |_|j dS)N) rrTr0FileNotFoundErrorrrrrrsr-flushr.r2rt)rrwrrrreopenIfNeededs  "    z!WatchedFileHandler.reopenIfNeededcCs|jtjj||dS)N)rzrr r)rrrrrrszWatchedFileHandler.emit)r(NF)r$r%r&r rtrzrrrrrrqs rqc@sNeZdZddZdddZddZdd Zd d Zd d ZddZ ddZ dS) SocketHandlercCsZtjj|||_||_|dkr(||_n ||f|_d|_d|_d|_d|_ d|_ d|_ dS)NFg?g>@g@) rHandlerr hostportaddresssock closeOnError retryTime retryStartretryMax retryFactor)rr}r~rrrr s  zSocketHandler.__init__r+c Csj|jdk rtj|j|d}nJtjtjtj}|j|y|j|jWntk rd|j YnX|S)N)timeout) r~socketZcreate_connectionrAF_UNIX SOCK_STREAMZ settimeoutconnectOSErrorr.)rrrrrr makeSocket s  zSocketHandler.makeSocketc Cstj}|jdkrd}n ||jk}|ry|j|_d|_WnVtk r|jdkr^|j|_n"|j|j|_|j|jkr|j|_||j|_YnXdS)NT) rUrrrrrZ retryPeriodrr)rZnowZattemptrrr createSockets       zSocketHandler.createSocketc CsR|jdkr|j|jrNy|jj|Wn$tk rL|jjd|_YnXdS)N)rrsendallrr.)rrprrrsend8s  zSocketHandler.sendcCsj|j}|r|j|}t|j}|j|d<d|d<d|d<|jddtj|d}tj dt |}||S)Nr:argsexc_infomessager+z>L) rr6dict__dict__Z getMessagepoppickledumpsstructZpackr9)rrZeiZdummydrpZslenrrr makePickleKs     zSocketHandler.makePicklecCs0|jr|jr|jjd|_ntjj||dS)N)rrr.rr|r)rrrrrras  zSocketHandler.handleErrorc Cs<y|j|}|j|Wntk r6|j|YnXdS)N)rrrr)rrrprrrros  zSocketHandler.emitc Cs@|jz(|j}|r"d|_|jtjj|Wd|jXdS)N)acquirerr.rr|release)rrrrrr.~szSocketHandler.closeN)r+) r$r%r&r rrrrrrr.rrrrr{s  r{c@s$eZdZddZddZddZdS)DatagramHandlercCstj|||d|_dS)NF)r{r r)rr}r~rrrr szDatagramHandler.__init__cCs*|jdkrtj}ntj}tj|tj}|S)N)r~rrZAF_INET SOCK_DGRAM)rZfamilyrprrrrs  zDatagramHandler.makeSocketcCs&|jdkr|j|jj||jdS)N)rrsendtor)rrprrrrs zDatagramHandler.sendN)r$r%r&r rrrrrrrs  rc@seZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZdZdZdZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZeeee eeee eeeed Zeeeeee ee eeee eeeeeeeeedZ ddddddZ!de"fe dfdd Z#d!d"Z$d#d$Z%d%d&Z&d'd(Z'd)Z(d*Z)d+d,Z*dS)- SysLogHandlerrr+r5rYrZr[r\rC ) ZalertZcritcriticaldebugZemergerrerrorinfoZnoticeZpanicwarnwarning)ZauthZauthprivZcrondaemonZftpZkernZlprZmailZnewsZsecurityZsysloguserZuucpZlocal0Zlocal1Zlocal2Zlocal3Zlocal4Zlocal5Zlocal6Zlocal7rrrrr)DEBUGINFOWARNINGERRORCRITICALZ localhostNcCs0tjj|||_||_||_t|trTd|_y|j |Wnt k rPYnXnd|_|dkrht j }|\}}t j ||d|}|st dx|D]|}|\}}} } } d} } y(t j ||| } |t jkr| j| PWqt k r }z|} | dk r| jWYdd}~XqXqW| dk r | | |_ ||_dS)NTFrz!getaddrinfo returns an empty list)rr|r rfacilitysocktype isinstancestr unixsocket_connect_unixsocketrrrZ getaddrinforrr.)rrrrr}r~ZressresZafproto_Zsarrexcrrrr sB      zSysLogHandler.__init__cCs|j}|dkrtj}tjtj||_y|jj|||_Wnxtk r|jj|jdk r`tj}tjtj||_y|jj|||_Wn tk r|jjYnXYnXdS)N)rrrrrrr.r)rrZ use_socktyperrrrSs&       z!SysLogHandler._connect_unixsocketcCs4t|tr|j|}t|tr(|j|}|d>|BS)NrY)rrfacility_namespriority_names)rrZpriorityrrrencodePriorityks     zSysLogHandler.encodePriorityc Cs2|jz|jjtjj|Wd|jXdS)N)rrr.rr|r)rrrrr.xs  zSysLogHandler.closecCs|jj|dS)Nr) priority_mapget)rZ levelNamerrr mapPriorityszSysLogHandler.mapPriorityTcCsy|j|}|jr|j|}|jr*|d7}d|j|j|j|j}|jd}|jd}||}|jry|j j |Wqt k r|j j |j |j|j j |YqXn*|jt jkr|j j||jn |j j|Wntk r|j|YnXdS)Nz<%d>zutf-8)r6ident append_nulrrrZ levelnameencoderrrrr.rrrrrrrr)rrr:Zpriorrrrs.        zSysLogHandler.emit)+r$r%r&Z LOG_EMERGZ LOG_ALERTZLOG_CRITZLOG_ERRZ LOG_WARNINGZ LOG_NOTICEZLOG_INFOZ LOG_DEBUGZLOG_KERNZLOG_USERZLOG_MAILZ LOG_DAEMONZLOG_AUTHZ LOG_SYSLOGZLOG_LPRZLOG_NEWSZLOG_UUCPZLOG_CRONZ LOG_AUTHPRIVZLOG_FTPZ LOG_LOCAL0Z LOG_LOCAL1Z LOG_LOCAL2Z LOG_LOCAL3Z LOG_LOCAL4Z LOG_LOCAL5Z LOG_LOCAL6Z LOG_LOCAL7rrrSYSLOG_UDP_PORTr rrr.rrrrrrrrrs5   rc@s&eZdZd ddZddZddZdS) SMTPHandlerN@cCstjj|t|ttfr(|\|_|_n|d|_|_t|ttfrR|\|_|_ nd|_||_ t|t rn|g}||_ ||_ ||_||_dS)N)rr|r rlisttuplemailhostmailportusernamepasswordfromaddrrtoaddrssubjectsecurer)rrrrr credentialsrrrrrr s  zSMTPHandler.__init__cCs|jS)N)r)rrrrr getSubjectszSMTPHandler.getSubjectc Csyddl}ddlm}ddl}|j}|s.|j}|j|j||jd}|}|j |d<dj |j |d<|j ||d<|j j|d<|j|j||jr|jdk r|j|j|j|j|j|j|j|j||jWntk r|j|YnXdS) Nr) EmailMessage)rZFrom,ZToZSubjectZDate)smtplibZ email.messagerZ email.utilsrZ SMTP_PORTZSMTPrrrrlrrZutilsr^Z set_contentr6rrZehloZstarttlsZloginrZ send_messagequitrr)rrrrZemailr~Zsmtpr:rrrrs0      zSMTPHandler.emit)NNr)r$r%r&r rrrrrrrs " rc@s>eZdZdddZddZddZd d Zd d Zd dZdS)NTEventLogHandlerN ApplicationcCstjj|yddl}ddl}||_||_|s`tjj |jj }tjj |d}tjj |dd}||_ ||_ |jj||||j|_tj|jtj|jtj|jtj|jtj|ji|_Wn"tk rtdd|_YnXdS)Nrzwin32service.pydzWThe Python Win32 extensions for NT (service, event logging) appear not to be available.)rr|r win32evtlogutil win32evtlogappname_welurrrh__file__rldllnamelogtypeZAddSourceToRegistryZEVENTLOG_ERROR_TYPEdeftyperZEVENTLOG_INFORMATION_TYPErrZEVENTLOG_WARNING_TYPErrtypemap ImportErrorprint)rrrrrrrrrr s* zNTEventLogHandler.__init__cCsdS)Nr+r)rrrrr getMessageID(szNTEventLogHandler.getMessageIDcCsdS)Nrr)rrrrrgetEventCategory2sz"NTEventLogHandler.getEventCategorycCs|jj|j|jS)N)rrlevelnor)rrrrr getEventType;s zNTEventLogHandler.getEventTypec Csn|jrjyD|j|}|j|}|j|}|j|}|jj|j||||gWntk rh|j|YnXdS)N) rrrrr6Z ReportEventrrr)rridcattyper:rrrrHs    zNTEventLogHandler.emitcCstjj|dS)N)rr|r.)rrrrr.Ys zNTEventLogHandler.close)Nr) r$r%r&r rrrrr.rrrrrs     rc@s&eZdZd ddZddZdd ZdS) HTTPHandlerGETFNcCsbtjj||j}|dkr$td| r:|dk r:td||_||_||_||_||_ ||_ dS)NrPOSTzmethod must be GET or POSTz3context parameter only makes sense with secure=True)rr) rr|r rFrNr}urlmethodrrcontext)rr}rrrrrrrrr ks zHTTPHandler.__init__cCs|jS)N)r)rrrrr mapLogRecordszHTTPHandler.mapLogRecordc CsxyPddl}ddl}|j}|jr4|jj||jd}n |jj|}|j}|j j |j |}|j dkr|j ddkrvd}nd}|d||f}|j|j ||j d} | dkr|d| }|j dkr|jd d |jd tt||jr$ddl} d |jjd } d| j| jjd} |jd| |j|j dkrH|j|jd |jWn tk rr|j|YnXdS)Nr)rr?&z%c%s:rz Content-typez!application/x-www-form-urlencodedzContent-lengthz%s:%szutf-8zBasic asciiZ Authorization)Z http.clientZ urllib.parser}rZclientZHTTPSConnectionrZHTTPConnectionrparseZ urlencoderrfindZ putrequestZ putheaderrr9rbase64rZ b64encodestripdecodeZ endheadersrZ getresponserr) rrZhttpZurllibr}r<rdatasepr3rrprrrrs@        zHTTPHandler.emit)rFNN)r$r%r&r rrrrrrrfs rc@s4eZdZddZddZddZddZd d Zd S) BufferingHandlercCstjj|||_g|_dS)N)rr|r capacitybuffer)rr rrrr s zBufferingHandler.__init__cCst|j|jkS)N)r9r r )rrrrr shouldFlushszBufferingHandler.shouldFlushcCs"|jj||j|r|jdS)N)r rkr ry)rrrrrrs  zBufferingHandler.emitc Cs"|jz g|_Wd|jXdS)N)rr r)rrrrrys zBufferingHandler.flushc Cs z |jWdtjj|XdS)N)ryrr|r.)rrrrr.s zBufferingHandler.closeN)r$r%r&r r rryr.rrrrr s    r c@s>eZdZejddfddZddZddZd d Zd d Z dS) MemoryHandlerNTcCs"tj||||_||_||_dS)N)r r flushLeveltarget flushOnClose)rr rrrrrrr s zMemoryHandler.__init__cCst|j|jkp|j|jkS)N)r9r r rr)rrrrrr szMemoryHandler.shouldFlushcCs ||_dS)N)r)rrrrr setTargetszMemoryHandler.setTargetc CsD|jz,|jr2x|jD]}|jj|qWg|_Wd|jXdS)N)rrr handler)rrrrrrys  zMemoryHandler.flushcCsBz|jr|jWd|jzd|_tj|Wd|jXXdS)N)rryrrr r.r)rrrrr.&s zMemoryHandler.close) r$r%r&rrr r rryr.rrrrr s  r c@s,eZdZddZddZddZddZd S) QueueHandlercCstjj|||_dS)N)rr|r queue)rrrrrr Bs zQueueHandler.__init__cCs|jj|dS)N)r put_nowait)rrrrrenqueueIszQueueHandler.enqueuecCs"|j||j|_d|_d|_|S)N)r6rr:rr)rrrrrprepareSs  zQueueHandler.preparec Cs8y|j|j|Wntk r2|j|YnXdS)N)rrrr)rrrrrrlszQueueHandler.emitN)r$r%r&r rrrrrrrr7s  rc@sVeZdZdZddddZddZdd Zd d Zd d ZddZ ddZ ddZ dS) QueueListenerNF)respect_handler_levelcGs||_||_d|_||_dS)N)rhandlers_threadr)rrrrrrrr szQueueListener.__init__cCs |jj|S)N)rr)rblockrrrdequeueszQueueListener.dequeuecCs&tj|jd|_}d|_|jdS)N)rT) threadingZThread_monitorrrstart)rrXrrrr szQueueListener.startcCs|S)Nr)rrrrrrszQueueListener.preparecCsD|j|}x4|jD]*}|js"d}n |j|jk}|r|j|qWdS)NT)rrrrlevelr)rrZhandlerZprocessrrrrs   zQueueListener.handlec Csd|j}t|d}xNy0|jd}||jkr*P|j||r@|jWqtjk rZPYqXqWdS)N task_doneT)rhasattrr _sentinelrr"ZEmpty)rqZ has_task_donerrrrrs     zQueueListener._monitorcCs|jj|jdS)N)rrr$)rrrrenqueue_sentinelszQueueListener.enqueue_sentinelcCs|j|jjd|_dS)N)r&rrl)rrrrstops zQueueListener.stop) r$r%r&r$r rr rrrr&r'rrrrrxs     riiQ)&rrrrrrUrQrTrrrrrrZDEFAULT_TCP_LOGGING_PORTZDEFAULT_UDP_LOGGING_PORTZDEFAULT_HTTP_LOGGING_PORTZDEFAULT_SOAP_LOGGING_PORTrZSYSLOG_TCP_PORTr_r rr'r;rqr|r{rrrrrr r robjectrrrrrs@8  FL`E(*PbO9I@