graypy.handler module

Logging Handlers that send messages in Graylog Extended Log Format (GELF)

class graypy.handler.BaseGELFHandler(chunk_size=1420, debugging_fields=True, extra_fields=True, fqdn=False, localname=None, facility=None, level_names=False, compress=True)[source]

Bases: logging.Handler, abc.ABC

Abstract class defining the basic functionality of converting a logging.LogRecord into a GELF log. Provides the boilerplate for all GELF handlers defined within graypy.

__init__(chunk_size=1420, debugging_fields=True, extra_fields=True, fqdn=False, localname=None, facility=None, level_names=False, compress=True)[source]

Initialize the BaseGELFHandler.

Parameters
  • chunk_size (int) – Message chunk size. Messages larger than this size will be sent to Graylog in multiple chunks.

  • debugging_fields (bool) – If True add debug fields from the log record into the GELF logs to be sent to Graylog.

  • extra_fields (bool) – If True add extra fields from the log record into the GELF logs to be sent to Graylog.

  • fqdn (bool) – If True use the fully qualified domain name of localhost to populate the host GELF field.

  • localname (str or None) – If specified and fqdn is False, use the specified hostname to populate the host GELF field.

  • facility (str) – If specified, replace the facility GELF field with the specified value. Also add a additional _logger GELF field containing the LogRecord.name.

  • level_names (bool) – If True use python logging error level name strings instead of syslog numerical values.

  • compress (bool) – If True compress the GELF message before sending it to the Graylog server.

makePickle(record)[source]

Convert a logging.LogRecord into bytes representing a GELF log

Parameters

record (logging.LogRecord) – logging.LogRecord to convert into a GELF log.

Returns

bytes representing a GELF log.

Return type

bytes

class graypy.handler.ChunkedGELF(message, size)[source]

Bases: object

Class that chunks a message into a GELF compatible chunks

__init__(message, size)[source]

Initialize the ChunkedGELF message class

Parameters
  • message (bytes) – The message to chunk.

  • size (int) – The size of the chunks.

encode(sequence, chunk)[source]
message_chunks()[source]
class graypy.handler.GELFHTTPHandler(host, port=12203, compress=True, path='/gelf', timeout=5, **kwargs)[source]

Bases: graypy.handler.BaseGELFHandler

GELF HTTP handler

__init__(host, port=12203, compress=True, path='/gelf', timeout=5, **kwargs)[source]

Initialize the GELFHTTPHandler

Parameters
emit(record)[source]

Convert a logging.LogRecord to GELF and emit it to Graylog via a HTTP POST request

Parameters

record (logging.LogRecord) – logging.LogRecord to convert into a GELF log and emit to Graylog via a HTTP POST request.

class graypy.handler.GELFTCPHandler(host, port=12201, **kwargs)[source]

Bases: graypy.handler.BaseGELFHandler, logging.handlers.SocketHandler

GELF TCP handler

__init__(host, port=12201, **kwargs)[source]

Initialize the GELFTCPHandler

Parameters
  • host (str) – GELF TCP input host.

  • port (int) – GELF TCP input port.

Attention

GELF TCP does not support compression due to the use of the null byte (\0) as frame delimiter.

Thus, handler.GELFTCPHandler does not support setting compress to True and is locked to False.

makePickle(record)[source]

Add a null terminator to generated pickles as TCP frame objects need to be null terminated

Parameters

record (logging.LogRecord) – logging.LogRecord to create a null terminated GELF log.

Returns

Null terminated bytes representing a GELF log.

Return type

bytes

class graypy.handler.GELFTLSHandler(host, port=12204, validate=False, ca_certs=None, certfile=None, keyfile=None, **kwargs)[source]

Bases: graypy.handler.GELFTCPHandler

GELF TCP handler with TLS support

__init__(host, port=12204, validate=False, ca_certs=None, certfile=None, keyfile=None, **kwargs)[source]

Initialize the GELFTLSHandler

Parameters
  • host (str) – GELF TLS input host.

  • port (int) – GELF TLS input port.

  • validate (bool) – If True, validate the Graylog server’s certificate. In this case specifying ca_certs is also required.

  • ca_certs (str) – Path to CA bundle file.

  • certfile (str) – Path to the client certificate file.

  • keyfile (str) – Path to the client private key. If the private key is stored with the certificate, this parameter can be ignored.

makeSocket(timeout=1)[source]

Create a TLS wrapped socket

class graypy.handler.GELFUDPHandler(host, port=12202, **kwargs)[source]

Bases: graypy.handler.BaseGELFHandler, logging.handlers.DatagramHandler

GELF UDP handler

__init__(host, port=12202, **kwargs)[source]

Initialize the GELFUDPHandler

Parameters
  • host (str) – GELF UDP input host.

  • port (int) – GELF UDP input port.

send(s)[source]

Send a pickled string to a socket.

This function no longer allows for partial sends which can happen when the network is busy - UDP does not guarantee delivery and can deliver packets out of sequence.