graypy


Namegraypy JSON
Version 2.1.0 PyPI version JSON
download
home_pagehttps://github.com/severb/graypy
SummaryPython logging handlers that send messages in the Graylog Extended Log Format (GELF).
upload_time2019-09-30 22:39:26
maintainer
docs_urlNone
authorSever Banesiu
requires_python
licenseBSD License
keywords logging gelf graylog2 graylog udp amqp
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ######
graypy
######

.. image:: https://img.shields.io/pypi/v/graypy.svg
    :target: https://pypi.python.org/pypi/graypy
    :alt: PyPI Status

.. image:: https://travis-ci.org/severb/graypy.svg?branch=master
    :target: https://travis-ci.org/severb/graypy
    :alt: Build Status

.. image:: https://readthedocs.org/projects/graypy/badge/?version=stable
    :target: https://graypy.readthedocs.io/en/stable/?badge=stable
    :alt: Documentation Status

.. image:: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/severb/graypy
    :alt: 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:

.. code-block:: console

    pip install graypy

Install with requirements for ``GELFRabbitHandler``:

.. code-block:: console

    pip install graypy[amqp]

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

Install the basic graypy python logging handlers:

.. code-block:: console

    easy_install graypy

Install with requirements for ``GELFRabbitHandler``:

.. code-block:: console

    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``:

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


UDP GELF Chunkers
^^^^^^^^^^^^^^^^^

`GELF UDP Chunking`_ is supported by the ``GELFUDPHandler`` and is defined by
the ``gelf_chunker`` argument within its constructor. By default the
``GELFWarningChunker`` is used, thus, GELF messages that chunk overflow
(i.e. consisting of more than 128 chunks) will issue a
``GELFChunkOverflowWarning`` and **will be dropped**.

Other ``gelf_chunker`` options are also available:

* ``BaseGELFChunker`` silently drops GELF messages that chunk overflow
* ``GELFTruncatingChunker`` issues a ``GELFChunkOverflowWarning`` and
  simplifies and truncates GELF messages that chunk overflow in a attempt
  to send some content to Graylog. If this process fails to prevent
  another chunk overflow a ``GELFTruncationFailureWarning`` is issued.

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

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

Django Logging
--------------

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

.. code-block:: python

    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:

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

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:

.. code-block:: python

    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:

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

Using Filter
^^^^^^^^^^^^

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

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

Contributors
============

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

.. _GELF: https://docs.graylog.org/en/latest/pages/gelf.html
.. _logging.Handler: https://docs.python.org/3/library/logging.html#logging.Handler
.. _GELF UDP Chunking: https://docs.graylog.org/en/latest/pages/gelf.html#chunking
.. _LoggerAdapter: https://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information
.. _Filter: https://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/severb/graypy",
    "name": "graypy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "logging gelf graylog2 graylog udp amqp",
    "author": "Sever Banesiu",
    "author_email": "banesiu.sever@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8c/13/9fd9d88a16d4333b784c0d24daf90cf93bac63c7ab031dc72d9425b7f106/graypy-2.1.0.tar.gz",
    "platform": "",
    "description": "######\ngraypy\n######\n\n.. image:: https://img.shields.io/pypi/v/graypy.svg\n    :target: https://pypi.python.org/pypi/graypy\n    :alt: PyPI Status\n\n.. image:: https://travis-ci.org/severb/graypy.svg?branch=master\n    :target: https://travis-ci.org/severb/graypy\n    :alt: Build Status\n\n.. image:: https://readthedocs.org/projects/graypy/badge/?version=stable\n    :target: https://graypy.readthedocs.io/en/stable/?badge=stable\n    :alt: Documentation Status\n\n.. image:: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/severb/graypy\n    :alt: Coverage Status\n\nDescription\n===========\n\nPython logging handlers that send log messages in the\nGraylog Extended Log Format (GELF_).\n\ngraypy supports sending GELF logs to both Graylog2 and Graylog3 servers.\n\nInstalling\n==========\n\nUsing pip\n---------\n\nInstall the basic graypy python logging handlers:\n\n.. code-block:: console\n\n    pip install graypy\n\nInstall with requirements for ``GELFRabbitHandler``:\n\n.. code-block:: console\n\n    pip install graypy[amqp]\n\nUsing easy_install\n------------------\n\nInstall the basic graypy python logging handlers:\n\n.. code-block:: console\n\n    easy_install graypy\n\nInstall with requirements for ``GELFRabbitHandler``:\n\n.. code-block:: console\n\n    easy_install graypy[amqp]\n\nUsage\n=====\n\ngraypy sends GELF logs to a Graylog server via subclasses of the python\n`logging.Handler`_ class.\n\nBelow is the list of ready to run GELF logging handlers defined by graypy:\n\n* ``GELFUDPHandler`` - UDP log forwarding\n* ``GELFTCPHandler`` - TCP log forwarding\n* ``GELFTLSHandler`` - TCP log forwarding with TLS support\n* ``GELFHTTPHandler`` - HTTP log forwarding\n* ``GELFRabbitHandler`` - RabbitMQ log forwarding\n\nUDP Logging\n-----------\n\nUDP Log forwarding to a locally hosted Graylog server can be easily done with\nthe ``GELFUDPHandler``:\n\n.. code-block:: python\n\n    import logging\n    import graypy\n\n    my_logger = logging.getLogger('test_logger')\n    my_logger.setLevel(logging.DEBUG)\n\n    handler = graypy.GELFUDPHandler('localhost', 12201)\n    my_logger.addHandler(handler)\n\n    my_logger.debug('Hello Graylog.')\n\n\nUDP GELF Chunkers\n^^^^^^^^^^^^^^^^^\n\n`GELF UDP Chunking`_ is supported by the ``GELFUDPHandler`` and is defined by\nthe ``gelf_chunker`` argument within its constructor. By default the\n``GELFWarningChunker`` is used, thus, GELF messages that chunk overflow\n(i.e. consisting of more than 128 chunks) will issue a\n``GELFChunkOverflowWarning`` and **will be dropped**.\n\nOther ``gelf_chunker`` options are also available:\n\n* ``BaseGELFChunker`` silently drops GELF messages that chunk overflow\n* ``GELFTruncatingChunker`` issues a ``GELFChunkOverflowWarning`` and\n  simplifies and truncates GELF messages that chunk overflow in a attempt\n  to send some content to Graylog. If this process fails to prevent\n  another chunk overflow a ``GELFTruncationFailureWarning`` is issued.\n\nRabbitMQ Logging\n----------------\n\nAlternately, use ``GELFRabbitHandler`` to send messages to RabbitMQ and\nconfigure your Graylog server to consume messages via AMQP. This prevents log\nmessages from being lost due to dropped UDP packets (``GELFUDPHandler`` sends\nmessages to Graylog using UDP). You will need to configure RabbitMQ with a\n``gelf_log`` queue and bind it to the ``logging.gelf`` exchange so messages\nare properly routed to a queue that can be consumed by Graylog (the queue and\nexchange names may be customized to your liking).\n\n.. code-block:: python\n\n    import logging\n    import graypy\n\n    my_logger = logging.getLogger('test_logger')\n    my_logger.setLevel(logging.DEBUG)\n\n    handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf')\n    my_logger.addHandler(handler)\n\n    my_logger.debug('Hello Graylog.')\n\nDjango Logging\n--------------\n\nIt's easy to integrate ``graypy`` with Django's logging settings. Just add a\nnew handler in your ``settings.py``:\n\n.. code-block:: python\n\n    LOGGING = {\n        'version': 1,\n        # other dictConfig keys here...\n        'handlers': {\n            'graypy': {\n                'level': 'WARNING',\n                'class': 'graypy.GELFUDPHandler',\n                'host': 'localhost',\n                'port': 12201,\n            },\n        },\n        'loggers': {\n            'django.request': {\n                'handlers': ['graypy'],\n                'level': 'ERROR',\n                'propagate': True,\n            },\n        },\n    }\n\nTraceback Logging\n-----------------\n\nBy default log captured exception tracebacks are added to the GELF log as\n``full_message`` fields:\n\n.. code-block:: python\n\n    import logging\n    import graypy\n\n    my_logger = logging.getLogger('test_logger')\n    my_logger.setLevel(logging.DEBUG)\n\n    handler = graypy.GELFUDPHandler('localhost', 12201)\n    my_logger.addHandler(handler)\n\n    try:\n        puff_the_magic_dragon()\n    except NameError:\n        my_logger.debug('No dragons here.', exc_info=1)\n\nDefault Logging Fields\n----------------------\n\nBy default a number of debugging logging fields are automatically added to the\nGELF log if available:\n\n    * function\n    * pid\n    * process_name\n    * thread_name\n\nYou can disable automatically adding these debugging logging fields by\nspecifying ``debugging_fields=False`` in the handler's constructor:\n\n.. code-block:: python\n\n    handler = graypy.GELFUDPHandler('localhost', 12201, debugging_fields=False)\n\nAdding Custom Logging Fields\n----------------------------\n\ngraypy also supports including custom fields in the GELF logs sent to Graylog.\nThis can be done by using Python's LoggerAdapter_ and Filter_ classes.\n\nUsing LoggerAdapter\n^^^^^^^^^^^^^^^^^^^\n\nLoggerAdapter_ makes it easy to add static information to your GELF log\nmessages:\n\n.. code-block:: python\n\n    import logging\n    import graypy\n\n    my_logger = logging.getLogger('test_logger')\n    my_logger.setLevel(logging.DEBUG)\n\n    handler = graypy.GELFUDPHandler('localhost', 12201)\n    my_logger.addHandler(handler)\n\n    my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'),\n                                       {'username': 'John'})\n\n    my_adapter.debug('Hello Graylog from John.')\n\nUsing Filter\n^^^^^^^^^^^^\n\nFilter_ gives more flexibility and allows for dynamic information to be\nadded to your GELF logs:\n\n.. code-block:: python\n\n    import logging\n    import graypy\n\n    class UsernameFilter(logging.Filter):\n        def __init__(self):\n            # In an actual use case would dynamically get this\n            # (e.g. from memcache)\n            self.username = 'John'\n\n        def filter(self, record):\n            record.username = self.username\n            return True\n\n    my_logger = logging.getLogger('test_logger')\n    my_logger.setLevel(logging.DEBUG)\n\n    handler = graypy.GELFUDPHandler('localhost', 12201)\n    my_logger.addHandler(handler)\n\n    my_logger.addFilter(UsernameFilter())\n\n    my_logger.debug('Hello Graylog from John.')\n\nContributors\n============\n\n  * Sever Banesiu\n  * Daniel Miller\n  * Tushar Makkar\n  * Nathan Klapstein\n\n.. _GELF: https://docs.graylog.org/en/latest/pages/gelf.html\n.. _logging.Handler: https://docs.python.org/3/library/logging.html#logging.Handler\n.. _GELF UDP Chunking: https://docs.graylog.org/en/latest/pages/gelf.html#chunking\n.. _LoggerAdapter: https://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information\n.. _Filter: https://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information\n\n\n",
    "bugtrack_url": null,
    "license": "BSD License",
    "summary": "Python logging handlers that send messages in the Graylog Extended Log Format (GELF).",
    "version": "2.1.0",
    "project_urls": {
        "Homepage": "https://github.com/severb/graypy"
    },
    "split_keywords": [
        "logging",
        "gelf",
        "graylog2",
        "graylog",
        "udp",
        "amqp"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8280d9de7f4747ab54aad84c479d2c3dad9171de5a7f832ce4229bcef1f472ce",
                "md5": "2bb0120d4db56496fd631ac0bd08bbaa",
                "sha256": "5df0102ed52fdaa24dd579bc1e4904480c2c9bbb98917a0b3241ecf510c94207"
            },
            "downloads": -1,
            "filename": "graypy-2.1.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2bb0120d4db56496fd631ac0bd08bbaa",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 29907,
            "upload_time": "2019-09-30T22:39:24",
            "upload_time_iso_8601": "2019-09-30T22:39:24.619598Z",
            "url": "https://files.pythonhosted.org/packages/82/80/d9de7f4747ab54aad84c479d2c3dad9171de5a7f832ce4229bcef1f472ce/graypy-2.1.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c139fd9d88a16d4333b784c0d24daf90cf93bac63c7ab031dc72d9425b7f106",
                "md5": "4d795fad43069e27b7823b7402aa64ac",
                "sha256": "fd8dc4a721de1278576d92db10ac015e99b4e480cf1b18892e79429fd9236e16"
            },
            "downloads": -1,
            "filename": "graypy-2.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4d795fad43069e27b7823b7402aa64ac",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 24187,
            "upload_time": "2019-09-30T22:39:26",
            "upload_time_iso_8601": "2019-09-30T22:39:26.658780Z",
            "url": "https://files.pythonhosted.org/packages/8c/13/9fd9d88a16d4333b784c0d24daf90cf93bac63c7ab031dc72d9425b7f106/graypy-2.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-09-30 22:39:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "severb",
    "github_project": "graypy",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "graypy"
}
        
Elapsed time: 0.19135s