### Install graypy with easy_install Source: https://graypy.readthedocs.io/en/latest/readme Installs the basic graypy Python logging handlers using easy_install. ```bash easy_install graypy ``` -------------------------------- ### Install graypy with pip Source: https://graypy.readthedocs.io/en/latest/readme Installs the basic graypy Python logging handlers using pip. ```bash pip install graypy ``` -------------------------------- ### Install graypy with easy_install for RabbitMQ Source: https://graypy.readthedocs.io/en/latest/readme Installs graypy with the necessary requirements for the GELFRabbitHandler using easy_install. ```bash easy_install graypy[amqp] ``` -------------------------------- ### Install graypy with pip for RabbitMQ Source: https://graypy.readthedocs.io/en/latest/readme Installs graypy with the necessary requirements for the GELFRabbitHandler, which is used for RabbitMQ logging. ```bash pip install graypy[amqp] ``` -------------------------------- ### Django Logging Configuration with GELFUDPHandler Source: https://graypy.readthedocs.io/en/latest/readme Shows how to integrate graypy with Django's logging settings by defining a new handler in `settings.py`. This example configures a GELFUDPHandler to send WARNING level logs to a Graylog instance running on localhost. ```django LOGGING = { 'version': 1, # other dictConfig keys here... 'handlers': { 'graypy': { 'level': 'WARNING', 'class': 'graypy.GELFUDPHandler', 'host': 'localhost', 'port': 12201, }, }, 'loggers': { 'django.request': { 'handlers': ['graypy'], 'level': 'ERROR', 'propagate': True, }, }, } ``` -------------------------------- ### RabbitMQ GELF Handler Source: https://graypy.readthedocs.io/en/latest/api/graypy.rabbitmq This section details the graypy.rabbitmq module, which offers handlers for integrating with RabbitMQ to send GELF formatted logs. It covers the necessary setup and usage for logging applications via RabbitMQ. ```APIDOC graypy.rabbitmq This module provides handlers for sending GELF messages via RabbitMQ. Classes: RMQHandler(host, port=5672, username=None, password=None, virtual_host='/', exchange='graylog', routing_key='graylog', level_names=False, fqdn=False, localname=None, tls=False, tls_verify=True, ca_certs=None, certfile=None, keyfile=None, extra_fields=True, **kwargs) A GELF handler that sends messages to a RabbitMQ exchange. Parameters: host (str): The RabbitMQ host. port (int): The RabbitMQ port (default: 5672). username (str, optional): Username for authentication. password (str, optional): Password for authentication. virtual_host (str): The virtual host to use (default: '/'). exchange (str): The AMQP exchange to publish to (default: 'graylog'). routing_key (str): The routing key to use (default: 'graylog'). level_names (bool): If True, use level names instead of numbers (default: False). fqdn (bool): If True, use the fully qualified domain name for the host (default: False). localname (str, optional): The local hostname to use in messages. tls (bool): If True, use TLS encryption (default: False). tls_verify (bool): If True, verify TLS certificates (default: True). ca_certs (str, optional): Path to CA certificates file. certfile (str, optional): Path to client certificate file. keyfile (str, optional): Path to client private key file. extra_fields (bool): If True, include extra fields in the GELF message (default: True). **kwargs: Additional keyword arguments. Methods: emit(record): Emits a log record. close(): Closes the handler. GELFUDPHandler(host, port=12201, level_names=False, fqdn=False, localname=None, extra_fields=True, **kwargs) A GELF handler that sends messages to a UDP port. Parameters: host (str): The Graylog host. port (int): The Graylog UDP port (default: 12201). level_names (bool): If True, use level names instead of numbers (default: False). fqdn (bool): If True, use the fully qualified domain name for the host (default: False). localname (str, optional): The local hostname to use in messages. extra_fields (bool): If True, include extra fields in the GELF message (default: True). **kwargs: Additional keyword arguments. Methods: emit(record): Emits a log record. close(): Closes the handler. GELFHTTPHandler(host, port=12201, url='/gelf', timeout=5, level_names=False, fqdn=False, localname=None, extra_fields=True, **kwargs) A GELF handler that sends messages to an HTTP input. Parameters: host (str): The Graylog host. port (int): The Graylog HTTP port (default: 12201). url (str): The GELF endpoint URL (default: '/gelf'). timeout (int): The HTTP request timeout in seconds (default: 5). level_names (bool): If True, use level names instead of numbers (default: False). fqdn (bool): If True, use the fully qualified domain name for the host (default: False). localname (str, optional): The local hostname to use in messages. extra_fields (bool): If True, include extra fields in the GELF message (default: True). **kwargs: Additional keyword arguments. Methods: emit(record): Emits a log record. close(): Closes the handler. GELFTLSHandler(host, port=12201, url='/gelf', timeout=5, level_names=False, fqdn=False, localname=None, extra_fields=True, tls_verify=True, ca_certs=None, certfile=None, keyfile=None, **kwargs) A GELF handler that sends messages to an HTTPS input. Parameters: host (str): The Graylog host. port (int): The Graylog HTTPS port (default: 12201). url (str): The GELF endpoint URL (default: '/gelf'). timeout (int): The HTTP request timeout in seconds (default: 5). level_names (bool): If True, use level names instead of numbers (default: False). fqdn (bool): If True, use the fully qualified domain name for the host (default: False). localname (str, optional): The local hostname to use in messages. extra_fields (bool): If True, include extra fields in the GELF message (default: True). tls_verify (bool): If True, verify TLS certificates (default: True). ca_certs (str, optional): Path to CA certificates file. certfile (str, optional): Path to client certificate file. keyfile (str, optional): Path to client private key file. **kwargs: Additional keyword arguments. Methods: emit(record): Emits a log record. close(): Closes the handler. ``` -------------------------------- ### RabbitMQ Logging with GELFRabbitHandler Source: https://graypy.readthedocs.io/en/latest/readme Demonstrates how to send log messages to RabbitMQ using graypy's GELFRabbitHandler. This handler is useful for ensuring log messages are not lost due to UDP packet drops. It requires a RabbitMQ setup with a specific queue and exchange for Graylog consumption. ```python import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf') my_logger.addHandler(handler) my_logger.debug('Hello Graylog.') ``` -------------------------------- ### Graypy RabbitMQ Handler Initialization and Methods Source: https://graypy.readthedocs.io/en/latest/genindex Covers the initialization and key methods for graypy's RabbitMQ integration. This includes the ExcludeFilter class and the GELFRabbitHandler and RabbitSocket classes. ```APIDOC graypy.rabbitmq.ExcludeFilter.__init__(self, exclude_patterns) Initializes the ExcludeFilter with a list of patterns to exclude. graypy.rabbitmq.ExcludeFilter.filter(self, message) Filters a message based on the exclude patterns. Returns True if the message should be excluded, False otherwise. graypy.rabbitmq.GELFRabbitHandler.__init__(self, host, port, ...) Initializes the GELFRabbitHandler for sending GELF messages via RabbitMQ. graypy.rabbitmq.RabbitSocket.__init__(self, host, port, ...) Initializes the RabbitSocket for communication with RabbitMQ. graypy.rabbitmq.RabbitSocket.close(self) Closes the connection to RabbitMQ. ``` -------------------------------- ### GELFHTTPHandler Initialization and Methods Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Details the GELFHTTPHandler class for sending GELF messages via HTTP POST requests. It covers initialization parameters like host, port, compression, path, and timeout, as well as the emit method for sending log records. ```APIDOC class GELFHTTPHandler(BaseGELFHandler): """GELF HTTP handler""" def __init__(self, host, port=12203, compress=True, path='/gelf', timeout=5, **kwargs): """Initialize the GELFHTTPHandler :param host: GELF HTTP input host. :type host: str :param port: GELF HTTP input port. :type port: int :param compress: If :obj:`True` compress the GELF message before sending it to the Graylog server. :type compress: bool :param path: Path of the HTTP input. (see http://docs.graylog.org/en/latest/pages/sending_data.html#gelf-via-http) :type path: str :param timeout: Number of seconds the HTTP client should wait before it discards the request if the Graylog server doesn't respond. :type timeout: int """ # ... implementation details ... def emit(self, record): """Convert a :class:`logging.LogRecord` to GELF and emit it to Graylog via a HTTP POST request :param record: :class:`logging.LogRecord` to convert into a GELF log and emit to Graylog via a HTTP POST request. :type record: logging.LogRecord """ # ... implementation details ... ``` -------------------------------- ### ExcludeFilter.filter Method Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/rabbitmq Filters a log record based on the initialized name. It checks if the record's name starts with the filter's name and if the length matches or the next character is a dot. ```APIDOC filter(record: logging.LogRecord) -> bool Filters a log record. :param record: The log record to filter. :type record: logging.LogRecord :return: True if the record should be excluded, False otherwise. :rtype: bool ``` ```python def filter(self, record): return not (record.name.startswith(self.name) and ( len(record.name) == self.nlen or record.name[self.nlen] == ".")) ``` -------------------------------- ### BaseGELFHandler Initialization Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Initializes the BaseGELFHandler with various configuration options for GELF logging. It handles chunk size, debugging fields, extra fields, hostname resolution, facility settings, level name formatting, and message compression. ```APIDOC class BaseGELFHandler(logging.Handler, ABC): def __init__(self, chunk_size=WAN_CHUNK, debugging_fields=True, extra_fields=True, fqdn=False, localname=None, facility=None, level_names=False, compress=True): """Initialize the BaseGELFHandler. :param chunk_size: Message chunk size. Messages larger than this size will be sent to Graylog in multiple chunks. :type chunk_size: int :param debugging_fields: If :obj:`True` add debug fields from the log record into the GELF logs to be sent to Graylog. :type debugging_fields: bool :param extra_fields: If :obj:`True` add extra fields from the log record into the GELF logs to be sent to Graylog. :type extra_fields: bool :param fqdn: If :obj:`True` use the fully qualified domain name of localhost to populate the ``host`` GELF field. :type fqdn: bool :param localname: If specified and ``fqdn`` is :obj:`False`, use the specified hostname to populate the ``host`` GELF field. :type localname: str or None :param facility: If specified, replace the ``facility`` GELF field with the specified value. Also add a additional ``_logger`` GELF field containing the ``LogRecord.name``. :type facility: str :param level_names: If :obj:`True` use python logging error level name strings instead of syslog numerical values. :type level_names: bool :param compress: If :obj:`True` compress the GELF message before sending it to the Graylog server. :type compress: bool """ logging.Handler.__init__(self) self.debugging_fields = debugging_fields self.extra_fields = extra_fields self.chunk_size = chunk_size if fqdn and localname: raise ValueError( "cannot specify 'fqdn' and 'localname' arguments together") self.fqdn = fqdn self.localname = localname self.facility = facility self.level_names = level_names self.compress = compress ``` -------------------------------- ### graypy Package Overview Source: https://graypy.readthedocs.io/en/latest/api/graypy Provides Python logging handlers for sending messages in the Graylog Extended Log Format (GELF). Includes basic handlers and a RabbitMQ handler. ```python graypy Python logging handlers that send messages in the Graylog Extended Log Format (GELF). Modules: * graypy.handler - Basic GELF Logging Handlers * graypy.rabbitmq - RabbitMQ GELF Logging Handler ``` -------------------------------- ### Core Methods in graypy Handlers Source: https://graypy.readthedocs.io/en/latest/genindex Details on core methods used by graypy handlers for message processing and sending. This includes methods for pickling, socket creation, chunking messages, and the actual sending mechanism. ```APIDOC makePickle() Serializes a message into a pickle format. Used by: graypy.handler.BaseGELFHandler, graypy.handler.GELFTCPHandler, graypy.rabbitmq.GELFRabbitHandler makeSocket() Creates and returns a socket object. Used by: graypy.handler.GELFTLSHandler, graypy.rabbitmq.GELFRabbitHandler message_chunks() Splits a large message into smaller chunks. Used by: graypy.handler.ChunkedGELF send(data) Sends data over the established connection. Used by: graypy.handler.GELFUDPHandler sendall(data) Sends all data over the established connection. Used by: graypy.rabbitmq.RabbitSocket ``` -------------------------------- ### UDP Logging with GELFUDPHandler Source: https://graypy.readthedocs.io/en/latest/readme Demonstrates how to configure and use the GELFUDPHandler to send debug messages to a locally hosted Graylog server via UDP. ```python import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFUDPHandler('localhost', 12201) my_logger.addHandler(handler) my_logger.debug('Hello Graylog.') ``` -------------------------------- ### Graypy Handler Module Initialization Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Initializes the graypy.handler module, defining constants for UDP chunk sizes (WAN and LAN) and handling Python 2/3 compatibility for data and text types. It imports necessary modules for logging, networking, and data compression. ```python #!/usr/bin/env python # -*- coding: utf-8 -*- """Logging Handlers that send messages in Graylog Extended Log Format (GELF)""" import abc import datetime import json import logging import math import random import socket import ssl import struct import sys import traceback import zlib from logging.handlers import DatagramHandler, SocketHandler WAN_CHUNK = 1420 LAN_CHUNK = 8154 if sys.version_info[0] == 3: # check if python3+ data, text = bytes, str else: data, text = str, unicode # pylint: disable=undefined-variable ``` -------------------------------- ### GELFRabbitHandler Initialization and Configuration Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/rabbitmq Initializes the GELFRabbitHandler for sending GELF logs via RabbitMQ. It parses the RabbitMQ URL, sets up connection arguments, and configures the exchange and routing key. It also includes an ExcludeFilter to ignore messages from the amqplib library. ```python class GELFRabbitHandler(BaseGELFHandler, SocketHandler): """RabbitMQ / GELF handler .. note:: This handler ignores all messages logged by amqplib. """ def __init__(self, url, exchange='logging.gelf', exchange_type='fanout', virtual_host='/', routing_key='', **kwargs): """Initialize the GELFRabbitHandler :param url: RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/) :type url: str :param exchange: RabbitMQ exchange. A queue binding must be defined on the server to prevent GELF logs from being dropped. :type exchange: str :param exchange_type: RabbitMQ exchange type. :type exchange_type: str :param virtual_host: :type virtual_host: str :param routing_key: :type routing_key: str """ self.url = url parsed = urlparse(url) if parsed.scheme != 'amqp': raise ValueError('invalid URL scheme (expected "amqp"): %s' % url) host = parsed.hostname or 'localhost' port = _ifnone(parsed.port, 5672) self.virtual_host = virtual_host if not unquote( parsed.path[1:]) else unquote(parsed.path[1:]) self.cn_args = { 'host': '%s:%s' % (host, port), 'userid': _ifnone(parsed.username, 'guest'), 'password': _ifnone(parsed.password, 'guest'), 'virtual_host': self.virtual_host, 'insist': False, } self.exchange = exchange self.exchange_type = exchange_type self.routing_key = routing_key BaseGELFHandler.__init__( self, **kwargs ) SocketHandler.__init__(self, host, port) self.addFilter(ExcludeFilter('amqplib')) ``` -------------------------------- ### Graypy Modules Source: https://graypy.readthedocs.io/en/latest/py-modindex Lists the main modules available in the graypy library. Each entry links to the detailed API documentation for that module. ```python graypy graypy.handler graypy.rabbitmq ``` -------------------------------- ### graypy.handler module API Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler Documentation for the graypy.handler module, covering GELF handlers. This includes classes and methods for sending log messages to a Graylog server. ```APIDOC graypy.handler Provides GELF handlers for sending logs to Graylog. Classes: GELFUDPHandler(host, port=12201, validate=False, tls_verify=False, tls_ca_certs=None, tls_certfile=None, tls_keyfile=None, tls_ciphers=None, extra_fields=False, debugging_fields=False, fqdn=False, localname=None, defaults=None) UDP GELF handler. Parameters: host (str): Graylog host. port (int): Graylog port (default: 12201). validate (bool): Validate GELF messages (default: False). tls_verify (bool): Verify TLS certificates (default: False). tls_ca_certs (str): Path to CA certificates file. tls_certfile (str): Path to TLS certificate file. tls_keyfile (str): Path to TLS key file. tls_ciphers (str): TLS cipher suite. extra_fields (bool): Include extra fields in GELF messages (default: False). debugging_fields (bool): Include debugging fields (default: False). fqdn (bool): Use fully qualified domain name (default: False). localname (str): Local hostname (default: None). defaults (dict): Default fields to include in messages (default: None). GELFTCPHandler(host, port=12201, validate=False, tls_verify=False, tls_ca_certs=None, tls_certfile=None, tls_keyfile=None, tls_ciphers=None, extra_fields=False, debugging_fields=False, fqdn=False, localname=None, defaults=None) TCP GELF handler. Parameters: host (str): Graylog host. port (int): Graylog port (default: 12201). validate (bool): Validate GELF messages (default: False). tls_verify (bool): Verify TLS certificates (default: False). tls_ca_certs (str): Path to CA certificates file. tls_certfile (str): Path to TLS certificate file. tls_keyfile (str): Path to TLS key file. tls_ciphers (str): TLS cipher suite. extra_fields (bool): Include extra fields in GELF messages (default: False). debugging_fields (bool): Include debugging fields (default: False). fqdn (bool): Use fully qualified domain name (default: False). localname (str): Local hostname (default: None). defaults (dict): Default fields to include in messages (default: None). GELFHTTPHandler(host, port=12201, url='/gelf', validate=False, tls_verify=False, tls_ca_certs=None, tls_certfile=None, tls_keyfile=None, tls_ciphers=None, extra_fields=False, debugging_fields=False, fqdn=False, localname=None, defaults=None) HTTP GELF handler. Parameters: host (str): Graylog host. port (int): Graylog port (default: 12201). url (str): GELF endpoint URL (default: '/gelf'). validate (bool): Validate GELF messages (default: False). tls_verify (bool): Verify TLS certificates (default: False). tls_ca_certs (str): Path to CA certificates file. tls_certfile (str): Path to TLS certificate file. tls_keyfile (str): Path to TLS key file. tls_ciphers (str): TLS cipher suite. extra_fields (bool): Include extra fields in GELF messages (default: False). debugging_fields (bool): Include debugging fields (default: False). fqdn (bool): Use fully qualified domain name (default: False). localname (str): Local hostname (default: None). defaults (dict): Default fields to include in messages (default: None). makePickle(record) Create a pickle from a log record. Parameters: record (logging.LogRecord): The log record to pickle. Returns: bytes: The pickled log record. makeGELF(record) Create a GELF message from a log record. Parameters: record (logging.LogRecord): The log record to convert. Returns: dict: The GELF message. makeHTTPGELF(record) Create a GELF message for HTTP transport. Parameters: record (logging.LogRecord): The log record to convert. Returns: dict: The GELF message. makeTCPGELF(record) Create a GELF message for TCP transport. Parameters: record (logging.LogRecord): The log record to convert. Returns: dict: The GELF message. ``` -------------------------------- ### GELFUDPHandler Configuration Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler Details the configuration parameters for the GELFUDPHandler, specifically the 'port' setting for GELF UDP input. ```APIDOC port: _int_ GELF UDP input port. ``` -------------------------------- ### RabbitMQ Related Classes Source: https://graypy.readthedocs.io/en/latest/genindex Documentation for classes related to RabbitMQ integration within graypy, specifically for handling messages over RabbitMQ connections. ```APIDOC RabbitSocket (class in graypy.rabbitmq) Represents a socket connection for RabbitMQ communication. Includes methods like sendall for transmitting data. ``` -------------------------------- ### Graypy Handler Initialization Source: https://graypy.readthedocs.io/en/latest/genindex Details the initialization of various GELF handler classes within the graypy library. This includes BaseGELFHandler and its subclasses like ChunkedGELF, GELFHTTPHandler, GELFTCPHandler, GELFTLSHandler, and GELFUDPHandler. ```APIDOC graypy.handler.BaseGELFHandler.__init__(self, host, port, ...) Initializes the base GELF handler. graypy.handler.ChunkedGELF.__init__(self, host, port, ...) Initializes the ChunkedGELF handler for sending GELF messages in chunks. graypy.handler.GELFHTTPHandler.__init__(self, host, port, ...) Initializes the GELFHTTPHandler for sending GELF messages over HTTP. graypy.handler.GELFTCPHandler.__init__(self, host, port, ...) Initializes the GELFTCPHandler for sending GELF messages over TCP. graypy.handler.GELFTLSHandler.__init__(self, host, port, ...) Initializes the GELFTLSHandler for sending GELF messages over TLS/SSL. graypy.handler.GELFUDPHandler.__init__(self, host, port, ...) Initializes the GELFUDPHandler for sending GELF messages over UDP. ``` -------------------------------- ### GELFTLSHandler Initialization Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Initializes the GELFTLSHandler for GELF TLS input. It supports optional TLS certificate validation and client certificate configuration. Raises ValueError if validation is enabled without CA certificates or if a client key is provided without a client certificate. ```APIDOC class GELFTLSHandler(GELFTCPHandler): """GELF TCP handler with TLS support""" def __init__(self, host, port=12204, validate=False, ca_certs=None, certfile=None, keyfile=None, **kwargs): """Initialize the GELFTLSHandler :param host: GELF TLS input host. :type host: str :param port: GELF TLS input port. :type port: int :param validate: If :obj:`True`, validate the Graylog server's certificate. In this case specifying ``ca_certs`` is also required. :type validate: bool :param ca_certs: Path to CA bundle file. :type ca_certs: str :param certfile: Path to the client certificate file. :type certfile: str :param keyfile: Path to the client private key. If the private key is stored with the certificate, this parameter can be ignored. :type keyfile: str """ if validate and ca_certs is None: raise ValueError('CA bundle file path must be specified') if keyfile is not None and certfile is None: raise ValueError('certfile must be specified') GELFTCPHandler.__init__(self, host=host, port=port, **kwargs) self.ca_certs = ca_certs self.reqs = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE self.certfile = certfile self.keyfile = keyfile if keyfile else certfile ``` -------------------------------- ### BaseGELFHandler Initialization and Methods Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler The BaseGELFHandler is an abstract class for GELF handlers, converting logging records to GELF format. It supports chunking, debugging fields, extra fields, FQDN, local name, facility customization, level names, and compression. ```APIDOC class graypy.handler.BaseGELFHandler(_chunk_size=1420, _debugging_fields=True, _extra_fields=True, _fqdn=False, _localname=None, _facility=None, _level_names=False, _compress=True) 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) 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) 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 ``` -------------------------------- ### Graypy Handler Methods Source: https://graypy.readthedocs.io/en/latest/genindex Details specific methods used by graypy's GELF handlers, including message emission and data encoding. ```APIDOC graypy.handler.GELFHTTPHandler.emit(self, record) Emits a log record using the HTTP handler. graypy.handler.ChunkedGELF.encode(self, message) Encodes a message for chunked transmission. ``` -------------------------------- ### Basic GELF Handlers Source: https://graypy.readthedocs.io/en/latest/index Provides basic GELF handlers for sending logs to Graylog. This section likely details the classes and methods for standard GELF logging. ```python from graypy import GELFUDPHandler handler = GELFUDPHandler('graylog.example.com', 12201) ``` -------------------------------- ### RabbitSocket Class and Methods Source: https://graypy.readthedocs.io/en/latest/api/graypy.rabbitmq Documentation for the RabbitSocket class, including its constructor, close method, and sendall method. This class is used for sending data over a RabbitMQ socket. ```APIDOC RabbitSocket: A factory method which allows subclasses to define the precise type of socket they want. __init__(_cn_args_, _timeout_, _exchange_, _exchange_type_, _routing_key_) Initialize self. See help(type(self)) for accurate signature. close() Close the connection to the RabbitMQ socket sendall(_data_) Sends data over the RabbitMQ socket. ``` -------------------------------- ### GELFTCPHandler Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Handles sending GELF logs over TCP. It initializes with a host and optional port, and appends a null terminator to each log message for TCP framing. Compression is not supported. ```APIDOC class GELFTCPHandler(BaseGELFHandler, SocketHandler): """GELF TCP handler""" def __init__(self, host, port=12201, **kwargs): """Initialize the GELFTCPHandler :param host: GELF TCP input host. :type host: str :param port: GELF TCP input port. :type port: int .. attention:: GELF TCP does not support compression due to the use of the null byte (``\0``) as frame delimiter. Thus, :class:`.handler.GELFTCPHandler` does not support setting ``compress`` to :obj:`True` and is locked to :obj:`False`. """ BaseGELFHandler.__init__(self, compress=False, **kwargs) SocketHandler.__init__(self, host, port) def makePickle(self, record): """Add a null terminator to generated pickles as TCP frame objects need to be null terminated :param record: :class:`logging.LogRecord` to create a null terminated GELF log. :type record: logging.LogRecord :return: Null terminated bytes representing a GELF log. :rtype: bytes """ return BaseGELFHandler.makePickle(self, record) + b'\x00' ``` -------------------------------- ### BaseGELFHandler makePickle Method Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Converts a logging.LogRecord into bytes representing a GELF log. This method handles the core logic of transforming a log record into the GELF format, including compression if enabled. ```APIDOC def makePickle(self, record): """Convert a :class:`logging.LogRecord` into bytes representing a GELF log :param record: :class:`logging.LogRecord` to convert into a GELF log. :type record: logging.LogRecord :return: bytes representing a GELF log. :rtype: bytes """ gelf_dict = self._make_gelf_dict(record) packed = self._pack_gelf_dict(gelf_dict) pickle = zlib.compress(packed) if self.compress else packed return pickle ``` -------------------------------- ### HTTP Library Import Handling Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler This code snippet handles the import of the HTTP library, providing compatibility between Python 2 and Python 3. It attempts to import `httplib` first, and if that fails (indicating a Python 3 environment), it imports `http.client` as `httplib`. ```python try: import httplib except ImportError: import http.client as httplib ``` -------------------------------- ### Traceback Logging with GELFUDPHandler Source: https://graypy.readthedocs.io/en/latest/readme Illustrates how to capture and log exception tracebacks using graypy's GELFUDPHandler. When `exc_info=1` is passed to a logging method, the traceback is automatically added to the GELF log as the `full_message` field. ```python import logging import graypy my_logger = logging.getLogger('test_logger') my_logger.setLevel(logging.DEBUG) handler = graypy.GELFUDPHandler('localhost', 12201) my_logger.addHandler(handler) try: puff_the_magic_dragon() except NameError: my_logger.debug('No dragons here.', exc_info=1) ``` -------------------------------- ### GELFRabbitHandler Socket Creation Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/rabbitmq Defines the method to create a socket connection for the GELFRabbitHandler, utilizing the RabbitSocket class with provided connection arguments, timeout, exchange, exchange type, and routing key. ```python def makeSocket(self, timeout=1): return RabbitSocket(self.cn_args, timeout, self.exchange, self.exchange_type, self.routing_key) ``` -------------------------------- ### ExcludeFilter Initialization Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/rabbitmq Initializes the ExcludeFilter with a name to match against log record names. Raises a ValueError if the provided name is empty. ```APIDOC ExcludeFilter(name: str) Initialize the ExcludeFilter :param name: Name to match for within a :class:`logging.LogRecord`'s ``name`` field for filtering. :type name: str :raises ValueError: If name is empty. ``` ```python def __init__(self, name): """Initialize the ExcludeFilter :param name: Name to match for within a :class:`logging.LogRecord`'s ``name`` field for filtering. :type name: str """ if not name: raise ValueError('ExcludeFilter requires a non-empty name') Filter.__init__(self, name) ``` -------------------------------- ### GELFTLSHandler Socket Creation Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Creates a TLS-wrapped socket for secure communication with the GELF TLS input. It configures the socket with optional timeout and TLS parameters like CA certificates and client certificates. ```APIDOC def makeSocket(self, timeout=1): """Create a TLS wrapped socket""" 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 ``` -------------------------------- ### GELFTLSHandler Class and Initialization Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler The GELFTLSHandler extends GELFTCPHandler to provide TLS encryption for GELF logs. It supports certificate validation and client authentication. ```APIDOC GELFTLSHandler(host, port=12204, validate=False, ca_certs=None, certfile=None, keyfile=None, **kwargs) Bases: graypy.handler.GELFTCPHandler Initialize the GELFTLSHandler. Parameters: host (str): GELF TLS input host. port (int): GELF TLS input port (default: 12204). validate (bool): If True, validate the Graylog server’s certificate. Requires ca_certs. ca_certs (str): Path to CA bundle file. certfile (str): Path to the client certificate file. keyfile (str): Path to the client private key. ``` -------------------------------- ### BaseGELFHandler Utility Methods Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Provides utility methods for GELF handlers, including packing GELF dictionaries into JSON-encoded bytes, sanitizing objects to Unicode, and converting objects to their JSON string representation. ```python def _pack_gelf_dict(gelf_dict): """Convert a given ``gelf_dict`` into JSON-encoded UTF-8 bytes, thus, creating an uncompressed GELF log ready for consumption by Graylog. Since we cannot be 100% sure of what is contained in the ``gelf_dict`` we have to do some sanitation. :param gelf_dict: dictionary representing a GELF log. :type gelf_dict: dict :return: bytes representing a uncompressed GELF log. :rtype: bytes """ gelf_dict = BaseGELFHandler._sanitize_to_unicode(gelf_dict) packed = json.dumps( gelf_dict, separators=',:', default=BaseGELFHandler._object_to_json ) return packed.encode('utf-8') def _sanitize_to_unicode(obj): """Convert all strings records of the object to unicode :param obj: object to sanitize to unicode. :type obj: object :return: Unicode string representing the given object. :rtype: str """ if isinstance(obj, dict): return dict((BaseGELFHandler._sanitize_to_unicode(k), BaseGELFHandler._sanitize_to_unicode(v)) for k, v in obj.items()) if isinstance(obj, (list, tuple)): return obj.__class__([BaseGELFHandler._sanitize_to_unicode(i) for i in obj]) if isinstance(obj, data): obj = obj.decode('utf-8', errors='replace') return obj def _object_to_json(obj): """Convert objects that cannot be natively serialized into JSON into their string representation (for later JSON serialization). :class:`datetime.datetime` based objects will be converted into a ISO formatted timestamp string. :param obj: object to convert into a string representation. :type obj: object :return: String representing the given object. :rtype: str """ if isinstance(obj, datetime.datetime): return obj.isoformat() return repr(obj) ``` -------------------------------- ### BaseGELFHandler _make_gelf_dict Method Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Creates a dictionary representing a GELF log from a Python logging.LogRecord. This internal method constructs the base GELF structure and incorporates various fields based on handler configuration. ```APIDOC def _make_gelf_dict(self, record): """Create a dictionary representing a GELF log from a python :class:`logging.LogRecord` :param record: :class:`logging.LogRecord` to create a GELF log from. :type record: logging.LogRecord :return: dictionary representing a GELF log. :rtype: dict """ # construct the base GELF format gelf_dict = { 'version': "1.0", 'host': BaseGELFHandler._resolve_host(self.fqdn, self.localname), 'short_message': self.formatter.format(record) if self.formatter else record.getMessage(), 'timestamp': record.created, 'level': SYSLOG_LEVELS.get(record.levelno, record.levelno), 'facility': self.facility or record.name, } # add in specified optional extras self._add_full_message(gelf_dict, record) if self.level_names: self._add_level_names(gelf_dict, record) if self.facility is not None: self._set_custom_facility(gelf_dict, self.facility, record) if self.debugging_fields: self._add_debugging_fields(gelf_dict, record) if self.extra_fields: self._add_extra_fields(gelf_dict, record) return gelf_dict ``` -------------------------------- ### GELFTLSHandler makeSocket Method Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler The makeSocket method for GELFTLSHandler creates a TLS-wrapped socket for secure communication. ```APIDOC makeSocket(timeout=1) Create a TLS wrapped socket. ``` -------------------------------- ### GELF Handlers in graypy Source: https://graypy.readthedocs.io/en/latest/genindex This section lists the various GELF handlers available in the graypy library, including HTTP, RabbitMQ, TCP, TLS, and UDP handlers. Each handler is designed for specific transport mechanisms for sending GELF messages. ```APIDOC GELFHTTPHandler (class in graypy.handler) Handler for sending GELF messages over HTTP. GELFRabbitHandler (class in graypy.rabbitmq) Handler for sending GELF messages via RabbitMQ. GELFTCPHandler (class in graypy.handler) Handler for sending GELF messages over TCP. GELFTLSHandler (class in graypy.handler) Handler for sending GELF messages over TLS/SSL. GELFUDPHandler (class in graypy.handler) Handler for sending GELF messages over UDP. ``` -------------------------------- ### GELFUDPHandler Source: https://graypy.readthedocs.io/en/latest/_modules/graypy/handler Handles sending GELF logs over UDP. It initializes with a host and optional port, and sends logs either directly or chunked if they exceed the chunk size. ```APIDOC class GELFUDPHandler(BaseGELFHandler, DatagramHandler): """GELF UDP handler""" def __init__(self, host, port=12202, **kwargs): """Initialize the GELFUDPHandler :param host: GELF UDP input host. :type host: str :param port: GELF UDP input port. :type port: int """ BaseGELFHandler.__init__(self, **kwargs) DatagramHandler.__init__(self, host, port) def send(self, s): """Send log message. :param s: Log message to send. :type s: bytes """ if len(s) < self.chunk_size: DatagramHandler.send(self, s) else: for chunk in ChunkedGELF(s, self.chunk_size): DatagramHandler.send(self, chunk) ``` -------------------------------- ### GELFTCPHandler makePickle Method Source: https://graypy.readthedocs.io/en/latest/api/graypy.handler The makePickle method for GELFTCPHandler adds a null terminator to log records, which is necessary for TCP frame delimitation in GELF. ```APIDOC makePickle(record) Parameters: record (logging.LogRecord): The log record to process. Returns: bytes: Null-terminated bytes representing a GELF log. ```