U afa$@sddlmZddlmZddlZddlZddlZddlZddlmZmZm Z m Z ddl m Z m Z mZmZddlmZddlZz ddlZWnek rdZYnXddlZddlmZddlmZdd lmZdd lmZmZmZd Z d Z!d Z"ej#dej$dej%dej&dej'dej(dej)dej*diZ+e,e-Z.Gdddej/Z0Gdddej/Z1Gdddej2Z3GdddeZ4e-ddej5ej6ddfe7e7ee e7e fe7e7ee7ee7ddddZ8dS) )datetime) SysLogHandlerN)AF_INET SOCK_DGRAM SOCK_STREAM SocketType)AnyDictOptionalTuple)resource_stream) iso8601_utc)Envelope) ConfigLogData) PerfLogExtra DevLogExtra BALogExtraiz@cee:z<7>z<6>z<4>z<3>z<2>z<1>csTeZdZdZejejejejde e e e ddfddZ e j e ddd Z ZS) "VerticalServiceStructuredFormatterzSpecialized structured logging formatter for HFS vertical services. Takes a log record and configuration data and formats it according to the structure in the Envelope class, returning a json-formatted log message. )log_type log_sub_type datacenter environmentN)rrrrreturncsBtj||ttj|tj|tj|tj||_dSN) super__init__rsetZ TYPE_FIELDZSUB_TYPE_FIELDZDATACENTER_FIELDZENVIRONMENT_FIELD configLogData)selfrrrrargskwargs __class__H/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/primordial/log/__init__.pyr7sz+VerticalServiceStructuredFormatter.__init__recordrcCst||j}t|Sr)rZ getEnveloperCOOKIEZgetJSON)rr'Z logenveloper$r$r%formatGsz)VerticalServiceStructuredFormatter.format)__name__ __module__ __qualname____doc__r DEFAULT_TYPEDEFAULT_SUB_TYPEDEFAULT_DATACENTERDEFAULT_ENVIRONMENTstrrlogging LogRecordr) __classcell__r$r$r"r%r0srcs*eZdZdZejedfdd ZZS)VerticalServiceSystemdFormatterz4Systemd logging formatter for HFS vertical services.r&cst|jd}|t|S)N) PREFIX_MAPgetlevelnorr))rr'prefixr"r$r%r)Osz&VerticalServiceSystemdFormatter.format) r*r+r,r-r3r4r2r)r5r$r$r"r%r6Lsr6c@s"eZdZdZejedddZdS)#VerticalServiceRequestLoggingFilterz;A log filter for producing only Flask web request messages.r&cCs<tr8tr8t|dsi|_tjjd}|r8||jd<dS)Nextraz x-request-id request_idT)flaskZhas_request_contexthasattrr=requestheadersr9)rr'r>r$r$r%filterWs   z*VerticalServiceRequestLoggingFilter.filterN)r*r+r,r-r3r4boolrCr$r$r$r%r<Tsr<cseZdZdZddefejefee e e e fe e ddddZ ddfdd Zejdd d d Zddd dZddddZZS)HFSSyslogHandlerzA customized Log handler for HFS syslogs. Given a log record, emits a log message formatted according to the :class:`VerticalServiceStructuredFormatter` class. N localhost)identaddressfacilitysocktypercCsV|ttfkrtdtj||p0tdp0d|_||_ ||_ ||_ d|_ d|_ dS)zProduce a new HFS syslog handler. :param ident: :param address: :param facility: :raises TypeError: For socket types other than TCP or UDP z:HFSSyslogHandler only supports TCP and UDP AF_INET socketsZ SYSLOG_IDENTpythonNF)rr TypeErrorr3HandlerrosgetenvrGrHrIrJsocket unixsocket)rrGrHrIrJr$r$r%rhs  zHFSSyslogHandler.__init__)rcs |jdkr|tdS)z0Ensure socket object, as expected by base class.N)rP _set_socketrcloserr"r$r%rSs zHFSSyslogHandler.closer&c Cs0z||j||j}tt|jtj }t d}|j dk rH|j nd}d}d|t |||j||f}d}||} |d|d} | d} | d| d } z>|jdkr||jtkr|j| |jn |j| Wn&tk r|jd|_YnXWn tk r*||YnXdS) zEmit a log record. The record is formatted, and an RFC-compliant message is sent to the syslog server. No structured data segment is created and message id is always nil ('-'). :param record: The record to be emitted. HOSTNAMENr-z<%d>%d %s %s %s %d %s  asciiutf8)encodePriorityrI mapPriority levelnamer r fromtimestampcreatedpytzutcrNrOprocessSYSLOG_FORMAT_VERSIONrGr)encoderP_connect_socketrJrsendtorHsendallOSErrorrS Exception handleError) rr'prio timestamphostnamepidZ message_idheaderZstructured_datamessageZ ascii_partZ utf8_partZencoded_messager$r$r%emitsB      zHFSSyslogHandler.emitcCs0||jd|jtkr,|j|jdS)Ng?)rRrP settimeoutrJrconnectrHrTr$r$r%res  z HFSSyslogHandler._connect_socketcCstt|j|_dSr)rPrrJrTr$r$r%rRszHFSSyslogHandler._set_socket)r*r+r,r-SYSLOG_DEFAULT_PORTrLOG_USERrr r2r intrrrSr3r4rqrerRr5r$r$r"r%rEas   0rEz logging.yaml)namefilenameoverride_loggersrrrrrc Cs|dk r |ntj}|dk r"|ntj}tt||}|d D]2}| ddkrL||d<||d<||d<||d<qL|dk r|d  |t j |t d dS) a+ Initialize logging, updating the existing log config loggers dict with an override_loggers dict. It's possible, since we have many ways to run the system, that init_logging be called more than once. We allow this, but warn if so. :param name: package name where the logging YAML config file is located :param filename: filename of the logging YAML config :param override_loggers: a dict of overrides to the default log dictconfig :param log_type: :param log_sub_type: :param datacenter: :param environment: N formattersz()z1primordial.log.VerticalServiceStructuredFormatterrrrrloggersT)rr0upperr1yamlZ safe_loadr readdecodevaluesr9updater3config dictConfigcaptureWarnings) rwrxryrrrrr formatterr$r$r% init_loggings  r)9rlogging.handlersrr3logging.configrNrPrrrrtypingrr r r pkg_resourcesr r}r? ImportErrorr`Zprimordial.timeutilsr Zprimordial.log.enveloperZprimordial.log.envelopedatarZprimordial.log.extrarrrrtrcr(DEBUGINFONOTSETWARNINGWARNERRORCRITICALFATALr8 getLoggerr*LOG Formatterrr6Filterr<rEr.r/r2rr$r$r$r%sv          d