Skip to main content

Python logging handler that sends messages in GELF (Graylog Extended Log Format).

Project description

Build_Status

Installing

Using easy_install:

easy_install graypy

Install with requirements for GELFRabbitHandler:

easy_install graypy[amqp]

Usage

Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format:

import logging
import graypy

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

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

my_logger.debug('Hello Graylog2.')

Alternately, use GELFRabbitHandler to send messages to RabbitMQ and configure your Graylog2 server to consume messages via AMQP. This prevents log messages from being lost due to dropped UDP packets (GELFHandler sends messages to Graylog2 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 Graylog2 (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 Graylog2.')

Tracebacks are added as full messages:

import logging
import graypy

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

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

try:
    puff_the_magic_dragon()
except NameError:
    my_logger.debug('No dragons here.', exc_info=1)

Configuration parameters

GELFHandler:

  • host - the host of the graylog server.

  • port - the port of the graylog server (default 12201).

  • chunk_size - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default 1420).

  • debugging_fields - send debug fields if true (the default).

  • extra_fields - send extra fields on the log record to graylog if true (the default).

  • fqdn - use fully qualified domain name of localhost as source host (socket.getfqdn()).

  • localname - use specified hostname as source host.

  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.

  • level_names - allows the use of string error level names instead in addition to their numerical representation.

GELFTcpHandler:

  • host - the host of the graylog server.

  • port - the port of the graylog server (default 12201).

  • chunk_size - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default 1420).

  • debugging_fields - send debug fields if true (the default).

  • extra_fields - send extra fields on the log record to graylog if true (the default).

  • fqdn - use fully qualified domain name of localhost as source host (socket.getfqdn()).

  • localname - use specified hostname as source host.

  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.

  • level_names - allows the use of string error level names instead in addition to their numerical representation.

  • tls - use transport layer security on connection to graylog if true (not the default)

  • tls_server_name - if using TLS, specify the name of the host to which the connection is being made. if not specified, hostname checking will not be performed.

  • param tls_cafile - if using TLS, optionally specify a file with a set of certificate authority certificates to use in certificate validation.

  • param tls_capath - if using TLS, optionally specify a path to files with a set of certificate authority certificates to use in certificate validation.

  • param tls_cadata - if using TLS, optionally specify an object with a set of certificate authority certificates to use in certificate validation.

  • param tls_client_cert - if using TLS, optionally specify a certificate to authenticate the client to the graylog server.

  • param tls_client_key - if using TLS, optionally specify a key file corresponding to the client certificate.

  • param tls_client_password - if using TLS, optionally specify a password corresponding to the client key file.

GELFRabbitHandler:

  • url - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F).

  • exchange - RabbitMQ exchange. Default ‘logging.gelf’. A queue binding must be defined on the server to prevent log messages from being dropped.

  • debugging_fields - send debug fields if true (the default).

  • extra_fields - send extra fields on the log record to graylog if true (the default).

  • fqdn - use fully qualified domain name of localhost as source host - socket.getfqdn().

  • exchange_type - RabbitMQ exchange type (default fanout).

  • localname - use specified hostname as source host.

  • facility - replace facility with specified value. if specified, record.name will be passed as logger parameter.

Using with Django

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

LOGGING = {
    ...

    'handlers': {
        'graypy': {
            'level': 'WARNING',
            'class': 'graypy.GELFHandler',
            'host': 'localhost',
            'port': 12201,
        },
    },

    'loggers': {
        'django.request': {
            'handlers': ['graypy'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

Custom fields

A number of custom fields are automatically added if available:
  • function

  • pid

  • process_name

  • thread_name

You can disable these additional fields if you don’t want them by adding an argument to the handler:

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

graypy also supports additional fields to be included in the messages sent to Graylog2. This can be done by using Python’s LoggerAdapter and Filter. In general, LoggerAdapter makes it easy to add static information to your log messages and Filters give you more flexibility, for example to add additional information based on the message that is being logged.

Example using LoggerAdapter:

import logging
import graypy

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

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

my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'),
                                    { 'username': 'John' })

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

Example using Filter:

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.GELFHandler('localhost', 12201)
my_logger.addHandler(handler)

my_logger.addFilter(UsernameFilter())

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

Contributors:

  • Sever Banesiu

  • Daniel Miller

  • Tushar Makkar

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-0.3.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

graypy-0.3.1-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: graypy-0.3.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for graypy-0.3.1.tar.gz
Algorithm Hash digest
SHA256 dc4d46d64d12ad7061b1bac6764f69e8f03d476acb26e84b655fb116779bc499
MD5 4c3d5b6fc61c6c39e03c822bf37904b5
BLAKE2b-256 c6cfda148350e6fe8ea923ac7e44a95bc1cde9cfcd2511e20b5e09a8393f68bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: graypy-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for graypy-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 13492a11caab912d7fe65ce2b95462887931b93f8d622e26f7c58fa5ac31581f
MD5 04a66d10e04929b9675b79f9e3b678aa
BLAKE2b-256 44664e9af3fa8b61d4c4ba562086fe1c6bbf3b74d795f25e423c23685af43b32

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