<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import json <EOL> import logging <EOL> import logging . handlers <EOL> import datetime <EOL> from salt . log . setup import LOG_LEVELS <EOL> from salt . log . mixins import NewStyleClassMixIn <EOL> import salt . utils . network <EOL> import salt . ext . six as six <EOL> try : <EOL> import zmq <EOL> except ImportError : <EOL> pass <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> if not any ( [ '<STR_LIT>' in __opts__ , <EOL> '<STR_LIT>' in __opts__ ] ) : <EOL> log . trace ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return False <EOL> return __virtualname__ <EOL> def setup_handlers ( ) : <EOL> host = port = address = None <EOL> if '<STR_LIT>' in __opts__ : <EOL> host = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT:host>' , None ) <EOL> port = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT:port>' , None ) <EOL> version = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT:version>' , <NUM_LIT:0> ) <EOL> if host is None and port is None : <EOL> log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> logstash_formatter = LogstashFormatter ( version = version ) <EOL> udp_handler = DatagramLogstashHandler ( host , port ) <EOL> udp_handler . setFormatter ( logstash_formatter ) <EOL> udp_handler . setLevel ( <EOL> LOG_LEVELS [ <EOL> __opts__ [ '<STR_LIT>' ] . get ( <EOL> '<STR_LIT>' , <EOL> __opts__ . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:error>' <EOL> ) <EOL> ) <EOL> ] <EOL> ) <EOL> yield udp_handler <EOL> if '<STR_LIT>' in __opts__ : <EOL> address = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT:address>' , None ) <EOL> zmq_hwm = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> version = __opts__ [ '<STR_LIT>' ] . get ( '<STR_LIT:version>' , <NUM_LIT:0> ) <EOL> if address is None : <EOL> log . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> logstash_formatter = LogstashFormatter ( version = version ) <EOL> zmq_handler = ZMQLogstashHander ( address , zmq_hwm = zmq_hwm ) <EOL> zmq_handler . setFormatter ( logstash_formatter ) <EOL> zmq_handler . setLevel ( <EOL> LOG_LEVELS [ <EOL> __opts__ [ '<STR_LIT>' ] . get ( <EOL> '<STR_LIT>' , <EOL> __opts__ . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:error>' <EOL> ) <EOL> ) <EOL> ] <EOL> ) <EOL> yield zmq_handler <EOL> if host is None and port is None and address is None : <EOL> yield False <EOL> class LogstashFormatter ( logging . Formatter , NewStyleClassMixIn ) : <EOL> def __init__ ( self , msg_type = '<STR_LIT>' , msg_path = '<STR_LIT>' , version = <NUM_LIT:0> ) : <EOL> self . msg_path = msg_path <EOL> self . msg_type = msg_type <EOL> self . version = version <EOL> self . format = getattr ( self , '<STR_LIT>' . format ( version ) ) <EOL> super ( LogstashFormatter , self ) . __init__ ( fmt = None , datefmt = None ) <EOL> def formatTime ( self , record , datefmt = None ) : <EOL> return datetime . datetime . utcfromtimestamp ( record . created ) . isoformat ( ) [ : - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> def format_v0 ( self , record ) : <EOL> host = salt . utils . network . get_fqhostname ( ) <EOL> message_dict = { <EOL> '<STR_LIT>' : self . formatTime ( record ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : record . levelname , <EOL> '<STR_LIT>' : record . name , <EOL> '<STR_LIT>' : record . lineno , <EOL> '<STR_LIT>' : record . pathname , <EOL> '<STR_LIT>' : record . process , <EOL> '<STR_LIT>' : record . threadName , <EOL> '<STR_LIT>' : record . funcName , <EOL> '<STR_LIT>' : record . processName <EOL> } , <EOL> '<STR_LIT>' : record . getMessage ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> self . msg_type , <EOL> host , <EOL> self . msg_path <EOL> ) , <EOL> '<STR_LIT>' : host , <EOL> '<STR_LIT>' : self . msg_path , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . msg_type , <EOL> } <EOL> if record . exc_info : <EOL> message_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . formatException ( <EOL> record . exc_info <EOL> ) <EOL> for key , value in six . iteritems ( record . __dict__ ) : <EOL> if key in ( '<STR_LIT:args>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:message>' , '<STR_LIT>' , <EOL> '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if value is None : <EOL> message_dict [ '<STR_LIT>' ] [ key ] = value <EOL> continue <EOL> if isinstance ( value , ( six . string_types , bool , dict , float , int , list ) ) : <EOL> message_dict [ '<STR_LIT>' ] [ key ] = value <EOL> continue <EOL> message_dict [ '<STR_LIT>' ] [ key ] = repr ( value ) <EOL> return json . dumps ( message_dict ) <EOL> def format_v1 ( self , record ) : <EOL> message_dict = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : self . formatTime ( record ) , <EOL> '<STR_LIT:host>' : salt . utils . network . get_fqhostname ( ) , <EOL> '<STR_LIT>' : record . levelname , <EOL> '<STR_LIT>' : record . name , <EOL> '<STR_LIT>' : record . lineno , <EOL> '<STR_LIT>' : record . pathname , <EOL> '<STR_LIT>' : record . process , <EOL> '<STR_LIT>' : record . threadName , <EOL> '<STR_LIT>' : record . funcName , <EOL> '<STR_LIT>' : record . processName , <EOL> '<STR_LIT:message>' : record . getMessage ( ) , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : self . msg_type <EOL> } <EOL> if record . exc_info : <EOL> message_dict [ '<STR_LIT>' ] = self . formatException ( <EOL> record . exc_info <EOL> ) <EOL> for key , value in six . iteritems ( record . __dict__ ) : <EOL> if key in ( '<STR_LIT:args>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:message>' , '<STR_LIT>' , <EOL> '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if value is None : <EOL> message_dict [ key ] = value <EOL> continue <EOL> if isinstance ( value , ( six . string_types , bool , dict , float , int , list ) ) : <EOL> message_dict [ key ] = value <EOL> continue <EOL> message_dict [ key ] = repr ( value ) <EOL> return json . dumps ( message_dict ) <EOL> class DatagramLogstashHandler ( logging . handlers . DatagramHandler ) : <EOL> '''<STR_LIT>''' <EOL> def makePickle ( self , record ) : <EOL> return self . format ( record ) <EOL> class ZMQLogstashHander ( logging . Handler , NewStyleClassMixIn ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , address , level = logging . NOTSET , zmq_hwm = <NUM_LIT:1000> ) : <EOL> super ( ZMQLogstashHander , self ) . __init__ ( level = level ) <EOL> self . _context = self . _publisher = None <EOL> self . _address = address <EOL> self . _zmq_hwm = zmq_hwm <EOL> self . _pid = os . getpid ( ) <EOL> @ property <EOL> def publisher ( self ) : <EOL> current_pid = os . getpid ( ) <EOL> if not getattr ( self , '<STR_LIT>' ) or self . _pid != current_pid : <EOL> self . _pid = current_pid <EOL> self . _context = zmq . Context ( ) <EOL> self . _publisher = self . _context . socket ( zmq . PUB ) <EOL> try : <EOL> self . _publisher . setsockopt ( zmq . HWM , self . _zmq_hwm ) <EOL> except AttributeError : <EOL> self . _publisher . setsockopt ( zmq . SNDHWM , self . _zmq_hwm ) <EOL> self . _publisher . setsockopt ( zmq . RCVHWM , self . _zmq_hwm ) <EOL> self . _publisher . connect ( self . _address ) <EOL> return self . _publisher <EOL> def emit ( self , record ) : <EOL> formatted_object = self . format ( record ) <EOL> self . publisher . send ( formatted_object ) <EOL> def close ( self ) : <EOL> if self . _context is not None : <EOL> if hasattr ( self . _context , '<STR_LIT>' ) : <EOL> self . _context . destroy ( <NUM_LIT:1> * <NUM_LIT:1000> ) <EOL> else : <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> self . _publisher . setsockopt ( zmq . LINGER , <NUM_LIT:1> * <NUM_LIT:1000> ) <EOL> self . _publisher . close ( ) <EOL> if self . _context . closed is False : <EOL> self . _context . term ( ) </s>
