METADATA 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Metadata-Version: 2.0
  2. Name: graypy
  3. Version: 0.3.1
  4. Summary: Python logging handler that sends messages in GELF (Graylog Extended Log Format).
  5. Home-page: https://github.com/severb/graypy
  6. Author: Sever Banesiu
  7. Author-email: banesiu.sever@gmail.com
  8. License: BSD License
  9. Description-Content-Type: UNKNOWN
  10. Keywords: logging gelf graylog2 graylog udp amqp
  11. Platform: UNKNOWN
  12. Classifier: License :: OSI Approved :: BSD License
  13. Classifier: Programming Language :: Python :: 2
  14. Classifier: Programming Language :: Python :: 3
  15. Provides-Extra: amqp
  16. Requires-Dist: amqplib (==1.0.2); extra == 'amqp'
  17. |Build_Status|
  18. Installing
  19. ==========
  20. Using easy_install::
  21. easy_install graypy
  22. Install with requirements for ``GELFRabbitHandler``::
  23. easy_install graypy[amqp]
  24. Usage
  25. =====
  26. Messages are sent to Graylog2 using a custom handler for the builtin logging library in GELF format::
  27. import logging
  28. import graypy
  29. my_logger = logging.getLogger('test_logger')
  30. my_logger.setLevel(logging.DEBUG)
  31. handler = graypy.GELFHandler('localhost', 12201)
  32. my_logger.addHandler(handler)
  33. my_logger.debug('Hello Graylog2.')
  34. 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)::
  35. import logging
  36. import graypy
  37. my_logger = logging.getLogger('test_logger')
  38. my_logger.setLevel(logging.DEBUG)
  39. handler = graypy.GELFRabbitHandler('amqp://guest:guest@localhost/', exchange='logging.gelf')
  40. my_logger.addHandler(handler)
  41. my_logger.debug('Hello Graylog2.')
  42. Tracebacks are added as full messages::
  43. import logging
  44. import graypy
  45. my_logger = logging.getLogger('test_logger')
  46. my_logger.setLevel(logging.DEBUG)
  47. handler = graypy.GELFHandler('localhost', 12201)
  48. my_logger.addHandler(handler)
  49. try:
  50. puff_the_magic_dragon()
  51. except NameError:
  52. my_logger.debug('No dragons here.', exc_info=1)
  53. Configuration parameters
  54. ========================
  55. GELFHandler:
  56. * **host** - the host of the graylog server.
  57. * **port** - the port of the graylog server (default 12201).
  58. * **chunk_size** - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default `1420`).
  59. * **debugging_fields** - send debug fields if true (the default).
  60. * **extra_fields** - send extra fields on the log record to graylog if true (the default).
  61. * **fqdn** - use fully qualified domain name of localhost as source host (socket.getfqdn()).
  62. * **localname** - use specified hostname as source host.
  63. * **facility** - replace facility with specified value. if specified, record.name will be passed as *logger* parameter.
  64. * **level_names** - allows the use of string error level names instead in addition to their numerical representation.
  65. GELFTcpHandler:
  66. * **host** - the host of the graylog server.
  67. * **port** - the port of the graylog server (default 12201).
  68. * **chunk_size** - message chunk size. messages larger than this size will be sent to graylog in multiple chunks (default `1420`).
  69. * **debugging_fields** - send debug fields if true (the default).
  70. * **extra_fields** - send extra fields on the log record to graylog if true (the default).
  71. * **fqdn** - use fully qualified domain name of localhost as source host (socket.getfqdn()).
  72. * **localname** - use specified hostname as source host.
  73. * **facility** - replace facility with specified value. if specified, record.name will be passed as *logger* parameter.
  74. * **level_names** - allows the use of string error level names instead in addition to their numerical representation.
  75. * **tls** - use transport layer security on connection to graylog if true (not the default)
  76. * **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.
  77. * **param tls_cafile** - if using TLS, optionally specify a file with a set of certificate authority certificates to use in certificate validation.
  78. * **param tls_capath** - if using TLS, optionally specify a path to files with a set of certificate authority certificates to use in certificate validation.
  79. * **param tls_cadata** - if using TLS, optionally specify an object with a set of certificate authority certificates to use in certificate validation.
  80. * **param tls_client_cert** - if using TLS, optionally specify a certificate to authenticate the client to the graylog server.
  81. * **param tls_client_key** - if using TLS, optionally specify a key file corresponding to the client certificate.
  82. * **param tls_client_password** - if using TLS, optionally specify a password corresponding to the client key file.
  83. GELFRabbitHandler:
  84. * **url** - RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/%2F).
  85. * **exchange** - RabbitMQ exchange. Default 'logging.gelf'. A queue binding must be defined on the server to prevent log messages from being dropped.
  86. * **debugging_fields** - send debug fields if true (the default).
  87. * **extra_fields** - send extra fields on the log record to graylog if true (the default).
  88. * **fqdn** - use fully qualified domain name of localhost as source host - socket.getfqdn().
  89. * **exchange_type** - RabbitMQ exchange type (default `fanout`).
  90. * **localname** - use specified hostname as source host.
  91. * **facility** - replace facility with specified value. if specified, record.name will be passed as `logger` parameter.
  92. Using with Django
  93. =================
  94. It's easy to integrate ``graypy`` with Django's logging settings. Just add a
  95. new handler in your ``settings.py`` like this::
  96. LOGGING = {
  97. ...
  98. 'handlers': {
  99. 'graypy': {
  100. 'level': 'WARNING',
  101. 'class': 'graypy.GELFHandler',
  102. 'host': 'localhost',
  103. 'port': 12201,
  104. },
  105. },
  106. 'loggers': {
  107. 'django.request': {
  108. 'handlers': ['graypy'],
  109. 'level': 'ERROR',
  110. 'propagate': True,
  111. },
  112. },
  113. }
  114. Custom fields
  115. =============
  116. A number of custom fields are automatically added if available:
  117. * function
  118. * pid
  119. * process_name
  120. * thread_name
  121. You can disable these additional fields if you don't want them by adding an argument to the handler::
  122. handler = graypy.GELFHandler('localhost', 12201, debugging_fields=False)
  123. 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.
  124. Example using LoggerAdapter_::
  125. import logging
  126. import graypy
  127. my_logger = logging.getLogger('test_logger')
  128. my_logger.setLevel(logging.DEBUG)
  129. handler = graypy.GELFHandler('localhost', 12201)
  130. my_logger.addHandler(handler)
  131. my_adapter = logging.LoggerAdapter(logging.getLogger('test_logger'),
  132. { 'username': 'John' })
  133. my_adapter.debug('Hello Graylog2 from John.')
  134. Example using Filter_::
  135. import logging
  136. import graypy
  137. class UsernameFilter(logging.Filter):
  138. def __init__(self):
  139. # In an actual use case would dynamically get this (e.g. from memcache)
  140. self.username = "John"
  141. def filter(self, record):
  142. record.username = self.username
  143. return True
  144. my_logger = logging.getLogger('test_logger')
  145. my_logger.setLevel(logging.DEBUG)
  146. handler = graypy.GELFHandler('localhost', 12201)
  147. my_logger.addHandler(handler)
  148. my_logger.addFilter(UsernameFilter())
  149. my_logger.debug('Hello Graylog2 from John.')
  150. Contributors:
  151. * Sever Banesiu
  152. * Daniel Miller
  153. * Tushar Makkar
  154. .. _LoggerAdapter: http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information
  155. .. _Filter: http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information
  156. .. |Build_Status| image:: https://travis-ci.org/severb/graypy.svg?branch=master
  157. :target: https://travis-ci.org/severb/graypy