Skip to main content

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

Project description

|PyPI_Status|
|Build_Status|
|Coverage_Status|

Description
===========

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

Installing
==========

Using pip
---------

Install the basic graypy python logging handlers

.. code-block:: bash

pip install graypy

Install with requirements for ``GELFRabbitHandler``

.. code-block:: bash

pip install graypy[amqp]

Using easy_install
------------------

Install the basic graypy python logging handlers

.. code-block:: bash

easy_install graypy

Install with requirements for ``GELFRabbitHandler``

.. code-block:: bash

easy_install graypy[amqp]

Usage
=====

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

.. code-block:: 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 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 (``GELFUDPHandler``
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)

.. code-block:: 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 Graylog2.')

Tracebacks are added as full messages

.. code-block:: 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)


For more detailed usage information please see the documentation provided
within graypy's handler's docstrings.

Using with Django
=================

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

.. code-block:: python

LOGGING = {
...

'handlers': {
'graypy': {
'level': 'WARNING',
'class': 'graypy.GELFUDPHandler',
'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 the ``debugging_fields=False`` to the handler:

.. code-block:: python

handler = graypy.GELFUDPHandler('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_

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

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

Example using Filter_

.. code-block:: python

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 Graylog2 from John.')

Contributors:

* Sever Banesiu
* Daniel Miller
* Tushar Makkar
* Nathan Klapstein

.. _GELF: http://docs.graylog.org/en/latest/pages/gelf.html
.. _LoggerAdapter: http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information
.. _Filter: http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information

.. |Build_Status| image:: https://travis-ci.org/severb/graypy.svg?branch=master
:target: https://travis-ci.org/severb/graypy


.. |Coverage_Status| image:: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg
:target: https://codecov.io/gh/severb/graypy

.. |PyPI_Status| image:: https://img.shields.io/pypi/v/graypy.svg
:target: https://pypi-hypernode.com/pypi/graypy

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

Uploaded Source

Built Distribution

graypy-1.1.1-py2.py3-none-any.whl (22.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: graypy-1.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14

File hashes

Hashes for graypy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 fdbd8157c66d9c2a18b8d57f7a344e63b6bd6fc8e601ec60e7f7e0d503f7ceed
MD5 6f0f4230285844267652cec939bd6816
BLAKE2b-256 6e82342f55b082722f21a00104e1142cc139931a1c7fc36c3205ccf382468b7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: graypy-1.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.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.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14

File hashes

Hashes for graypy-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fe54f1c525f99647c4876be1fe336649450bc4e894c44c97ec73d0a68d76110c
MD5 e217198a629d6c58d87d14d2d2c1450f
BLAKE2b-256 2c1fff46196a077937a069e931777a016a0af096220893602f47ffe65e0a6e46

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