Logging handler to send logs to your OpenSearch cluster with bulk SSL. Forked from https://github.com/logzio/logzio-python-handler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.2 KiB

8 years ago
9 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
  1. [![PyPI version](https://badge.fury.io/py/logzio-python-handler.svg)](https://badge.fury.io/py/logzio-python-handler) [![Build Status](https://travis-ci.org/logzio/logzio-python-handler.svg?branch=master)](https://travis-ci.org/logzio/logzio-python-handler)
  2. # The Logz.io Python Handler
  3. This is a Python handler that sends logs in bulk over HTTPS to Logz.io.
  4. The handler uses a subclass named LogzioSender (which can be used without this handler as well, to ship raw data).
  5. The LogzioSender class opens a new Thread, that consumes from the logs queue. Each iteration (its frequency of which can be configured by the logs_drain_timeout parameter), will try to consume the queue in its entirety.
  6. Logs will get divided into separate bulks, based on their size.
  7. LogzioSender will check if the main thread is alive. In case the main thread quits, it will try to consume the queue one last time, and then exit. So your program can hang for a few seconds, until the logs are drained.
  8. In case the logs failed to be sent to Logz.io after a couple of tries, they will be written to the local file system. You can later upload them to Logz.io using curl.
  9. ## Installation
  10. ```bash
  11. pip install logzio-python-handler
  12. ```
  13. ## Tested Python Versions
  14. Travis CI will build this handler and test against:
  15. - "2.7"
  16. - "3.4"
  17. - "3.5"
  18. - "3.6"
  19. We can't ensure compatibility to any other version, as we can't test it automatically.
  20. **Note**: The Logz.io Python Handler no longer tests Python 3.3 (which was [end-of-lifed](https://www.python.org/dev/peps/pep-0398/#id11) in 2017).
  21. To run tests:
  22. ```bash
  23. $ pip install tox
  24. $ tox
  25. ...
  26. ```
  27. ## Python configuration
  28. #### Config File
  29. ```
  30. [handlers]
  31. keys=LogzioHandler
  32. [handler_LogzioHandler]
  33. class=logzio.handler.LogzioHandler
  34. formatter=logzioFormat
  35. args=('token', 'my_type')
  36. [formatters]
  37. keys=logzioFormat
  38. [loggers]
  39. keys=root
  40. [logger_root]
  41. handlers=LogzioHandler
  42. level=INFO
  43. [formatter_logzioFormat]
  44. format={"additional_field": "value"}
  45. ```
  46. *args=() arguments, by order*
  47. - Your logz.io token
  48. - Log type, for searching in logz.io (defaults to "python")
  49. - Time to sleep between draining attempts (defaults to "3")
  50. - Logz.io Listener address (defaults to "https://listener.logz.io:8071")
  51. - Debug flag. Set to True, will print debug messages to stdout. (defaults to "False")
  52. - Backup logs flag. Set to False, will disable the local backup of logs in case of failure. (defaults to "True")
  53. - Network timeout, in seconds, int or float, for sending the logs to logz.io. (defaults to 10)
  54. Please note, that you have to configure those parameters by this exact order.
  55. i.e. you cannot set Debug to true, without configuring all of the previous parameters as well.
  56. #### Code Example
  57. ```python
  58. import logging
  59. import logging.config
  60. # Say i have saved my configuration under ./myconf.conf
  61. logging.config.fileConfig('myconf.conf')
  62. logger = logging.getLogger('superAwesomeLogzioLogger')
  63. logger.info('Test log')
  64. logger.warn('Warning')
  65. try:
  66. 1/0
  67. except:
  68. logger.exception("Supporting exceptions too!")
  69. ```
  70. #### Extra Fields
  71. In case you need to dynamic metadata to your logger, other then the constant metadata from the formatter, you can use the "extra" parameter.
  72. All key values in the dictionary passed in "extra" will be presented in Logz.io as new fields in the log you are sending.
  73. Please note, that you cannot override default fields by the python logger (i.e. lineno, thread, etc..)
  74. For example:
  75. ```
  76. logger.info('Warning', extra={'extra_key':'extra_value'})
  77. ```
  78. ## Django configuration
  79. ```
  80. LOGGING = {
  81. 'version': 1,
  82. 'disable_existing_loggers': False,
  83. 'formatters': {
  84. 'verbose': {
  85. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  86. },
  87. 'logzioFormat': {
  88. 'format': '{"additional_field": "value"}'
  89. }
  90. },
  91. 'handlers': {
  92. 'console': {
  93. 'class': 'logging.StreamHandler',
  94. 'level': 'DEBUG',
  95. 'formatter': 'verbose'
  96. },
  97. 'logzio': {
  98. 'class': 'logzio.handler.LogzioHandler',
  99. 'level': 'INFO',
  100. 'formatter': 'logzioFormat',
  101. 'token': 'token',
  102. 'logzio_type': "django",
  103. 'logs_drain_timeout': 5,
  104. 'url': 'https://listener.logz.io:8071',
  105. 'debug': True,
  106. 'network_timeout': 10,
  107. },
  108. },
  109. 'loggers': {
  110. 'django': {
  111. 'handlers': ['console', ],
  112. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO')
  113. },
  114. 'appname': {
  115. 'handlers': ['console', 'logzio'],
  116. 'level': 'INFO'
  117. }
  118. }
  119. }
  120. ```
  121. *Change*
  122. - token - Your logzio token
  123. - url - Logz.io Listener address
  124. - logs_drain_count - Number of logs to keep in buffer before draining
  125. - logs_drain_timeout - Time to wait before draining, regardless of the previouse setting
  126. - logzio_type - Log type, for searching in logz.io (defaults to "python"), it cannot contain a space.
  127. - appname - Your django app
  128. ## Release Notes
  129. - 2.0.12 - Support disable logs local backup
  130. - 2.0.11 - Completely isolate exception from the message
  131. - 2.0.10 - Not ignoring formatting on exceptions
  132. - 2.0.9 - Support extra fields on exceptions too (Thanks @asafc64!)
  133. - 2.0.8 - Various PEP8, testings and logging changes (Thanks @nir0s!)
  134. - 2.0.7 - Make sure sending thread is alive after fork (Thanks @jo-tham!)
  135. - 2.0.6 - Add "flush()" method to manually drain the queue (Thanks @orenmazor!)
  136. - 2.0.5 - Support for extra fields
  137. - 2.0.4 - Publish package as source along wheel, and supprt python3 packagin (Thanks @cchristous!)
  138. - 2.0.3 - Fix bug that consumed more logs while draining than Logz.io's bulk limit
  139. - 2.0.2 - Support for formatted messages (Thanks @johnraz!)
  140. - 2.0.1 - Added __all__ to __init__.py, so support * imports
  141. - 2.0.0 - Production, stable release.
  142. - *BREAKING* - Configuration option logs_drain_count was removed, and the order of the parameters has changed for better simplicity. Please review the parameters section above.
  143. - Introducing the LogzioSender class, which is generic and can be used without the handler wrap to ship raw data to Logz.io. Just create a new instance of the class, and use the append() method.
  144. - Simplifications and Robustness
  145. - Full testing framework
  146. - 1.X - Beta versions