graypy

2.1.0 · active · verified Tue Apr 14

graypy is a Python library that provides logging handlers for sending messages in the Graylog Extended Log Format (GELF). It integrates with Python's standard `logging` module and supports various transport protocols including UDP, TCP, TLS, HTTP, and RabbitMQ. The current version is 2.1.0, and while there isn't a strict release cadence, major versions have introduced breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure a basic UDP logging handler (`GELFUDPHandler`) to send messages to a Graylog server. It uses environment variables for the Graylog host and port, which is a common practice for configuration management. Ensure your Graylog server has a GELF UDP input configured on the specified port.

import logging
import graypy
import os

# Configure Graylog host and port, using environment variables for security
GRAYLOG_HOST = os.environ.get('GRAYLOG_HOST', 'localhost')
GRAYLOG_PORT = int(os.environ.get('GRAYLOG_PORT', 12201))

# Get a logger instance
my_logger = logging.getLogger('my_app_logger')
my_logger.setLevel(logging.DEBUG)

# Create a GELF UDP handler
handler = graypy.GELFUDPHandler(GRAYLOG_HOST, GRAYLOG_PORT)
my_logger.addHandler(handler)

# Send a log message
my_logger.debug('Hello Graylog from graypy!')
my_logger.info('This is an informational message.')
my_logger.warning('Something might be wrong here.')

try:
    1 / 0
except ZeroDivisionError:
    my_logger.exception('An error occurred!')

print(f"Log messages sent to Graylog at {GRAYLOG_HOST}:{GRAYLOG_PORT} (UDP).")

view raw JSON →