DESCRIPTION.rst 7.9 KB

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