Skip to main content

Python logging handlers that send messages in the Graylog Extended Log Format (GELF).

Project description

PyPI Status Build Status Documentation Status Coverage Status

Description

Python logging handlers that send log messages in the Graylog Extended Log Format (GELF).

graypy supports sending GELF logs to both Graylog2 and Graylog3 servers.

Installing

Using pip

Install the basic graypy python logging handlers:

pip install graypy

Install with requirements for GELFRabbitHandler:

pip install graypy[amqp]

Using easy_install

Install the basic graypy python logging handlers:

easy_install graypy

Install with requirements for GELFRabbitHandler:

easy_install graypy[amqp]

Usage

graypy sends GELF logs to a Graylog server via subclasses of the python logging.Handler class.

Below is the list of ready to run GELF logging handlers defined by graypy:

  • GELFUDPHandler - UDP log forwarding

  • GELFTCPHandler - TCP log forwarding

  • GELFTLSHandler - TCP log forwarding with TLS support

  • GELFHTTPHandler - HTTP log forwarding

  • GELFRabbitHandler - RabbitMQ log forwarding

UDP Logging

UDP Log forwarding to a locally hosted Graylog server can be easily done with the GELFUDPHandler:

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.')

RabbitMQ Logging

Alternately, use GELFRabbitHandler to send messages to RabbitMQ and configure your Graylog server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (GELFUDPHandler sends messages to Graylog using UDP). You will need to configure RabbitMQ with a gelf_log queue and bind it to the logging.gelf exchange so messages are properly routed to a queue that can be consumed by Graylog (the queue and exchange names may be customized to your liking).

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.')

Django Logging

It’s easy to integrate graypy with Django’s logging settings. Just add a new handler in your settings.py:

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,
        },
    },
}

Traceback Logging

By default log captured exception tracebacks are added to the GELF log as full_message fields:

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)

Default Logging Fields

By default a number of debugging logging fields are automatically added to the GELF log if available:

  • function

  • pid

  • process_name

  • thread_name

You can disable automatically adding these debugging logging fields by specifying debugging_fields=False in the handler’s constructor:

handler = graypy.GELFUDPHandler('localhost', 12201, debugging_fields=False)

Adding Custom Logging Fields

graypy also supports including custom fields in the GELF logs sent to Graylog. This can be done by using Python’s LoggerAdapter and Filter classes.

Using LoggerAdapter

LoggerAdapter makes it easy to add static information to your GELF log messages:

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_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'),
                                   {'username': 'John'})

my_adapter.debug('Hello Graylog from John.')

Using Filter

Filter gives more flexibility and allows for dynamic information to be added to your GELF logs:

import logging
import graypy

class UsernameFilter(logging.Filter):
    def __init__(self):
        # In an actual use case would dynamically get this
        # (e.g. from memcache)
        self.username = 'John'

    def filter(self, record):
        record.username = self.username
        return True

my_logger = logging.getLogger('test_logger')
my_logger.setLevel(logging.DEBUG)

handler = graypy.GELFUDPHandler('localhost', 12201)
my_logger.addHandler(handler)

my_logger.addFilter(UsernameFilter())

my_logger.debug('Hello Graylog from John.')

Contributors

  • Sever Banesiu

  • Daniel Miller

  • Tushar Makkar

  • Nathan Klapstein

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graypy-1.2.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

graypy-1.2.0-py2.py3-none-any.whl (27.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file graypy-1.2.0.tar.gz.

File metadata

  • Download URL: graypy-1.2.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3

File hashes

Hashes for graypy-1.2.0.tar.gz
Algorithm Hash digest
SHA256 edfa0e262dac0279a7ab54ffc4a0242d547844a763d2a76aa2e9cc4f433e60ea
MD5 97d6d723b40ca86f2c3691365e6e3cdd
BLAKE2b-256 ed6e8e14e059bfe435d9f52b61562b7d0cc347ed226270499fef166b56d8c9d6

See more details on using hashes here.

File details

Details for the file graypy-1.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: graypy-1.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3

File hashes

Hashes for graypy-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1b71dd52df92249272f4a88ebd62b18cc9e2dd26e5dccb509459a8a2d1c3fddd
MD5 5d58d35d62d327be2ddccf321804a538
BLAKE2b-256 e646745099ad906e3365d067a5794d5624fc78be35297d0f7c0630a6611fc8a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page