logging_gelf.handlers — Handlers

class logging_gelf.handlers.GELFUDPSocketHandler

New in version 0.0.7.

This handler send log entries over UDP.

makePickle(record)

Pickles the record’s attribute dictionary in binary format.

Parameters:record (logging.LogRecord) – record to format
Return type:bytes

Basic UDP example

>>> import logging
>>> from logging_gelf.handlers import GELFUDPSocketHandler

# we create the logger
>>> logger = logging.getLogger("gelf")
>>> logger.setLevel(logging.DEBUG)
>>> handler = GELFUDPSocketHandler(host="127.0.0.1", port=12202)
>>> logger.addHandler(handler)
class logging_gelf.handlers.GELFTCPSocketHandler

The GELFTCPSocketHandler, which inherit from logging.handlers.SocketHandler, sends logging output to a TCP network socket.

__init__(host, port, use_tls=False, cert_reqs=<ssl.CERT_NONE>, ca_certs=None)

Returns a new instance of the GELFTCPSocketHandler class intended to communicate with a remote machine whose address is given by host and port over TCP.

Parameters:
  • use_tls (bool) – Enable TLS communication.
  • cert_reqs (enum.IntEnum) – SSL context virify mode. This attribute must be one of ssl.CERT_NONE, ssl.CERT_OPTIONAL or ssl.CERT_REQUIRED (see ssl doc).
  • ca_certs (str) – File which contains a set of concatenated “certification authority” certificates, which are used to validate certificates passed from the other end of the connection.
makeSocket(timeout=1, after_idle_sec=1, interval_sec=3, max_fails=5)

Returns the socket used to send log records.

Parameters:
  • timeout (float) – Set a timeout on blocking socket operations, can be a nonnegative floating point number expressing seconds.
  • after_idle_sec (int) – Activates TCP keepalive after after_idle_sec second of idleness.
  • interval_sec (int) – Sends a keepalive ping once every interval_sec seconds.
  • max_fails (int) – Closes the connection after max_fails failed ping (= max_fails * interval_sec).
Returns:

a TCP socket.

Return type:

socket.socket

makePickle(record)

Pickles the record’s attribute dictionary in binary format.

Parameters:record (logging.LogRecord) – record to format
Return type:bytes

Basic TCP example

>>> import logging
>>> from logging_gelf.handlers import GELFTCPSocketHandler

# we create the logger
>>> logger = logging.getLogger("gelf")
>>> logger.setLevel(logging.DEBUG)
>>> handler = GELFTCPSocketHandler(host="127.0.0.1", port=12201, level=logging.DEBUG)
>>> logger.addHandler(handler)

See also

Logging handlers
Logging documentation
Socket Objects
Python socket documentation