| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: graypy |
| 3 |
Version: 2.1.0 |
| 4 |
Summary: Python logging handlers that send messages in the Graylog Extended Log Format (GELF). |
| 5 |
Home-page: https://github.com/severb/graypy |
| 6 |
Author: Sever Banesiu |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD License |
| 9 |
Description: ###### |
| 10 |
graypy |
| 11 |
###### |
| 12 |
|
| 13 |
.. image:: https://img.shields.io/pypi/v/graypy.svg |
| 14 |
:target: https://pypi.python.org/pypi/graypy |
| 15 |
:alt: PyPI Status |
| 16 |
|
| 17 |
.. image:: https://travis-ci.org/severb/graypy.svg?branch=master |
| 18 |
:target: https://travis-ci.org/severb/graypy |
| 19 |
:alt: Build Status |
| 20 |
|
| 21 |
.. image:: https://readthedocs.org/projects/graypy/badge/?version=stable |
| 22 |
:target: https://graypy.readthedocs.io/en/stable/?badge=stable |
| 23 |
:alt: Documentation Status |
| 24 |
|
| 25 |
.. image:: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg |
| 26 |
:target: https://codecov.io/gh/severb/graypy |
| 27 |
:alt: Coverage Status |
| 28 |
|
| 29 |
Description |
| 30 |
=========== |
| 31 |
|
| 32 |
Python logging handlers that send log messages in the |
| 33 |
Graylog Extended Log Format (GELF_). |
| 34 |
|
| 35 |
graypy supports sending GELF logs to both Graylog2 and Graylog3 servers. |
| 36 |
|
| 37 |
Installing |
| 38 |
========== |
| 39 |
|
| 40 |
Using pip |
| 41 |
--------- |
| 42 |
|
| 43 |
Install the basic graypy python logging handlers: |
| 44 |
|
| 45 |
.. code-block:: console |
| 46 |
|
| 47 |
pip install graypy |
| 48 |
|
| 49 |
Install with requirements for ``GELFRabbitHandler``: |
| 50 |
|
| 51 |
.. code-block:: console |
| 52 |
|
| 53 |
pip install graypy[amqp] |
| 54 |
|
| 55 |
Using easy_install |
| 56 |
------------------ |
| 57 |
|
| 58 |
Install the basic graypy python logging handlers: |
| 59 |
|
| 60 |
.. code-block:: console |
| 61 |
|
| 62 |
easy_install graypy |
| 63 |
|
| 64 |
Install with requirements for ``GELFRabbitHandler``: |
| 65 |
|
| 66 |
.. code-block:: console |
| 67 |
|
| 68 |
easy_install graypy[amqp] |
| 69 |
|
| 70 |
Usage |
| 71 |
===== |
| 72 |
|
| 73 |
graypy sends GELF logs to a Graylog server via subclasses of the python |
| 74 |
`logging.Handler`_ class. |
| 75 |
|
| 76 |
Below is the list of ready to run GELF logging handlers defined by graypy: |
| 77 |
|
| 78 |
* ``GELFUDPHandler`` - UDP log forwarding |
| 79 |
* ``GELFTCPHandler`` - TCP log forwarding |
| 80 |
* ``GELFTLSHandler`` - TCP log forwarding with TLS support |
| 81 |
* ``GELFHTTPHandler`` - HTTP log forwarding |
| 82 |
* ``GELFRabbitHandler`` - RabbitMQ log forwarding |
| 83 |
|
| 84 |
UDP Logging |
| 85 |
----------- |
| 86 |
|
| 87 |
UDP Log forwarding to a locally hosted Graylog server can be easily done with |
| 88 |
the ``GELFUDPHandler``: |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
import logging |
| 93 |
import graypy |
| 94 |
|
| 95 |
my_logger = logging.getLogger('test_logger') |
| 96 |
my_logger.setLevel(logging.DEBUG) |
| 97 |
|
| 98 |
handler = graypy.GELFUDPHandler('localhost', 12201) |
| 99 |
my_logger.addHandler(handler) |
| 100 |
|
| 101 |
my_logger.debug('Hello Graylog.') |
| 102 |
|
| 103 |
|
| 104 |
UDP GELF Chunkers |
| 105 |
^^^^^^^^^^^^^^^^^ |
| 106 |
|
| 107 |
`GELF UDP Chunking`_ is supported by the ``GELFUDPHandler`` and is defined by |
| 108 |
the ``gelf_chunker`` argument within its constructor. By default the |
| 109 |
``GELFWarningChunker`` is used, thus, GELF messages that chunk overflow |
| 110 |
(i.e. consisting of more than 128 chunks) will issue a |
| 111 |
``GELFChunkOverflowWarning`` and **will be dropped**. |
| 112 |
|
| 113 |
Other ``gelf_chunker`` options are also available: |
| 114 |
|
| 115 |
* ``BaseGELFChunker`` silently drops GELF messages that chunk overflow |
| 116 |
* ``GELFTruncatingChunker`` issues a ``GELFChunkOverflowWarning`` and |
| 117 |
simplifies and truncates GELF messages that chunk overflow in a attempt |
| 118 |
to send some content to Graylog. If this process fails to prevent |
| 119 |
another chunk overflow a ``GELFTruncationFailureWarning`` is issued. |
| 120 |
|
| 121 |
RabbitMQ Logging |
| 122 |
---------------- |
| 123 |
|
| 124 |
Alternately, use ``GELFRabbitHandler`` to send messages to RabbitMQ and |
| 125 |
configure your Graylog server to consume messages via AMQP. This prevents log |
| 126 |
messages from being lost due to dropped UDP packets (``GELFUDPHandler`` sends |
| 127 |
messages to Graylog using UDP). You will need to configure RabbitMQ with a |
| 128 |
``gelf_log`` queue and bind it to the ``logging.gelf`` exchange so messages |
| 129 |
are properly routed to a queue that can be consumed by Graylog (the queue and |
| 130 |
exchange names may be customized to your liking). |
| 131 |
|
| 132 |
.. code-block:: python |
| 133 |
|
| 134 |
import logging |
| 135 |
import graypy |
| 136 |
|
| 137 |
my_logger = logging.getLogger('test_logger') |
| 138 |
my_logger.setLevel(logging.DEBUG) |
| 139 |
|
| 140 |
handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf') |
| 141 |
my_logger.addHandler(handler) |
| 142 |
|
| 143 |
my_logger.debug('Hello Graylog.') |
| 144 |
|
| 145 |
Django Logging |
| 146 |
-------------- |
| 147 |
|
| 148 |
It's easy to integrate ``graypy`` with Django's logging settings. Just add a |
| 149 |
new handler in your ``settings.py``: |
| 150 |
|
| 151 |
.. code-block:: python |
| 152 |
|
| 153 |
LOGGING = { |
| 154 |
'version': 1, |
| 155 |
# other dictConfig keys here... |
| 156 |
'handlers': { |
| 157 |
'graypy': { |
| 158 |
'level': 'WARNING', |
| 159 |
'class': 'graypy.GELFUDPHandler', |
| 160 |
'host': 'localhost', |
| 161 |
'port': 12201, |
| 162 |
}, |
| 163 |
}, |
| 164 |
'loggers': { |
| 165 |
'django.request': { |
| 166 |
'handlers': ['graypy'], |
| 167 |
'level': 'ERROR', |
| 168 |
'propagate': True, |
| 169 |
}, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
Traceback Logging |
| 174 |
----------------- |
| 175 |
|
| 176 |
By default log captured exception tracebacks are added to the GELF log as |
| 177 |
``full_message`` fields: |
| 178 |
|
| 179 |
.. code-block:: python |
| 180 |
|
| 181 |
import logging |
| 182 |
import graypy |
| 183 |
|
| 184 |
my_logger = logging.getLogger('test_logger') |
| 185 |
my_logger.setLevel(logging.DEBUG) |
| 186 |
|
| 187 |
handler = graypy.GELFUDPHandler('localhost', 12201) |
| 188 |
my_logger.addHandler(handler) |
| 189 |
|
| 190 |
try: |
| 191 |
puff_the_magic_dragon() |
| 192 |
except NameError: |
| 193 |
my_logger.debug('No dragons here.', exc_info=1) |
| 194 |
|
| 195 |
Default Logging Fields |
| 196 |
---------------------- |
| 197 |
|
| 198 |
By default a number of debugging logging fields are automatically added to the |
| 199 |
GELF log if available: |
| 200 |
|
| 201 |
* function |
| 202 |
* pid |
| 203 |
* process_name |
| 204 |
* thread_name |
| 205 |
|
| 206 |
You can disable automatically adding these debugging logging fields by |
| 207 |
specifying ``debugging_fields=False`` in the handler's constructor: |
| 208 |
|
| 209 |
.. code-block:: python |
| 210 |
|
| 211 |
handler = graypy.GELFUDPHandler('localhost', 12201, debugging_fields=False) |
| 212 |
|
| 213 |
Adding Custom Logging Fields |
| 214 |
---------------------------- |
| 215 |
|
| 216 |
graypy also supports including custom fields in the GELF logs sent to Graylog. |
| 217 |
This can be done by using Python's LoggerAdapter_ and Filter_ classes. |
| 218 |
|
| 219 |
Using LoggerAdapter |
| 220 |
^^^^^^^^^^^^^^^^^^^ |
| 221 |
|
| 222 |
LoggerAdapter_ makes it easy to add static information to your GELF log |
| 223 |
messages: |
| 224 |
|
| 225 |
.. code-block:: python |
| 226 |
|
| 227 |
import logging |
| 228 |
import graypy |
| 229 |
|
| 230 |
my_logger = logging.getLogger('test_logger') |
| 231 |
my_logger.setLevel(logging.DEBUG) |
| 232 |
|
| 233 |
handler = graypy.GELFUDPHandler('localhost', 12201) |
| 234 |
my_logger.addHandler(handler) |
| 235 |
|
| 236 |
my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'), |
| 237 |
{'username': 'John'}) |
| 238 |
|
| 239 |
my_adapter.debug('Hello Graylog from John.') |
| 240 |
|
| 241 |
Using Filter |
| 242 |
^^^^^^^^^^^^ |
| 243 |
|
| 244 |
Filter_ gives more flexibility and allows for dynamic information to be |
| 245 |
added to your GELF logs: |
| 246 |
|
| 247 |
.. code-block:: python |
| 248 |
|
| 249 |
import logging |
| 250 |
import graypy |
| 251 |
|
| 252 |
class UsernameFilter(logging.Filter): |
| 253 |
def __init__(self): |
| 254 |
# In an actual use case would dynamically get this |
| 255 |
# (e.g. from memcache) |
| 256 |
self.username = 'John' |
| 257 |
|
| 258 |
def filter(self, record): |
| 259 |
record.username = self.username |
| 260 |
return True |
| 261 |
|
| 262 |
my_logger = logging.getLogger('test_logger') |
| 263 |
my_logger.setLevel(logging.DEBUG) |
| 264 |
|
| 265 |
handler = graypy.GELFUDPHandler('localhost', 12201) |
| 266 |
my_logger.addHandler(handler) |
| 267 |
|
| 268 |
my_logger.addFilter(UsernameFilter()) |
| 269 |
|
| 270 |
my_logger.debug('Hello Graylog from John.') |
| 271 |
|
| 272 |
Contributors |
| 273 |
============ |
| 274 |
|
| 275 |
* Sever Banesiu |
| 276 |
* Daniel Miller |
| 277 |
* Tushar Makkar |
| 278 |
* Nathan Klapstein |
| 279 |
|
| 280 |
.. _GELF: https://docs.graylog.org/en/latest/pages/gelf.html |
| 281 |
.. _logging.Handler: https://docs.python.org/3/library/logging.html#logging.Handler |
| 282 |
.. _GELF UDP Chunking: https://docs.graylog.org/en/latest/pages/gelf.html#chunking |
| 283 |
.. _LoggerAdapter: https://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information |
| 284 |
.. _Filter: https://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information |
| 285 |
|
| 286 |
Keywords: logging gelf graylog2 graylog udp amqp |
| 287 |
Platform: UNKNOWN |
| 288 |
Classifier: License :: OSI Approved :: BSD License |
| 289 |
Classifier: Intended Audience :: Developers |
| 290 |
Classifier: Programming Language :: Python |
| 291 |
Classifier: Programming Language :: Python :: 2 |
| 292 |
Classifier: Programming Language :: Python :: 2.7 |
| 293 |
Classifier: Programming Language :: Python :: 3 |
| 294 |
Classifier: Programming Language :: Python :: 3.2 |
| 295 |
Classifier: Programming Language :: Python :: 3.3 |
| 296 |
Classifier: Programming Language :: Python :: 3.4 |
| 297 |
Classifier: Programming Language :: Python :: 3.5 |
| 298 |
Classifier: Programming Language :: Python :: 3.6 |
| 299 |
Classifier: Programming Language :: Python :: 3.7 |
| 300 |
Classifier: Programming Language :: Python :: Implementation :: CPython |
| 301 |
Classifier: Programming Language :: Python :: Implementation :: PyPy |
| 302 |
Classifier: Topic :: System :: Logging |
| 303 |
Description-Content-Type: text/x-rst |
| 304 |
Provides-Extra: docs |
| 305 |
Provides-Extra: amqp |