Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.keyfile = keyfile if keyfile else certfile
def makeSocket(self, timeout=1):
plain_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if hasattr(plain_socket, 'settimeout'):
plain_socket.settimeout(timeout)
wrapped_socket = ssl.wrap_socket(plain_socket, ca_certs=self.ca_certs, cert_reqs=self.reqs,
keyfile=self.keyfile, certfile=self.certfile)
wrapped_socket.connect((self.host, self.port))
return wrapped_socket
class GelfHttpHandler(BaseHandler, LoggingHandler):
def __init__(self, host, port, compress=True, path='/gelf', timeout=5, **kwargs):
"""
Logging handler that transforms each record into GELF (graylog extended log format) and sends it over HTTP.
:param host: GELF HTTP input host
:param port: GELF HTTP input port
:param compress: compress message before sending it to the server or not
:param path: path of the HTTP input (http://docs.graylog.org/en/latest/pages/sending_data.html#gelf-via-http)
:param timeout: amount of seconds that HTTP client should wait before it discards the request
if the server doesn't respond
"""
LoggingHandler.__init__(self)
BaseHandler.__init__(self, compress=compress, **kwargs)
self.version = version
self.additional_fields = static_fields if static_fields else kwargs
self.include_extra_fields = include_extra_fields
self.additional_fields.pop('_id', None)
self.domain = socket.gethostname()
self.compress = compress
self.json_default = json_default
def convert_record_to_gelf(self, record):
return gelf.pack(
gelf.make(record, self.domain, self.debug, self.version, self.additional_fields, self.include_extra_fields),
self.compress, self.json_default
)
class GelfTcpHandler(BaseHandler, SocketHandler):
def __init__(self, host, port, **kwargs):
"""
Logging handler that transforms each record into GELF (graylog extended log format) and sends it over TCP.
:param host: GELF TCP input host
:param port: GELF TCP input port
"""
SocketHandler.__init__(self, host, port)
BaseHandler.__init__(self, **kwargs)
def makePickle(self, record):
""" if you send the message over tcp, it should always be null terminated or the input will reject it """
return self.convert_record_to_gelf(record) + b'\x00'
"""
Logging handler that transforms each record into GELF (graylog extended log format) and sends it over TCP.
:param host: GELF TCP input host
:param port: GELF TCP input port
"""
SocketHandler.__init__(self, host, port)
BaseHandler.__init__(self, **kwargs)
def makePickle(self, record):
""" if you send the message over tcp, it should always be null terminated or the input will reject it """
return self.convert_record_to_gelf(record) + b'\x00'
class GelfUdpHandler(BaseHandler, DatagramHandler):
def __init__(self, host, port, compress=True, chunk_size=1300, **kwargs):
"""
Logging handler that transforms each record into GELF (graylog extended log format) and sends it over UDP.
If message length exceeds chunk_size, the message splits into multiple chunks.
The number of chunks must be less than 128.
:param host: GELF UDP input host
:param port: GELF UDP input port
:param compress: compress message before sending it to the server or not
:param chunk_size: length of a chunk, should be less than the MTU (maximum transmission unit)
"""
DatagramHandler.__init__(self, host, port)
BaseHandler.__init__(self, compress=compress, **kwargs)