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.

168 lines
6.0 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. Please note, that you have to configure those parameters by this exact order.
  54. i.e. you cannot set Debug to true, without configuring all of the previous parameters as well.
  55. #### Code Example
  56. ```python
  57. import logging
  58. import logging.config
  59. # Say i have saved my configuration under ./myconf.conf
  60. logging.config.fileConfig('myconf.conf')
  61. logger = logging.getLogger('superAwesomeLogzioLogger')
  62. logger.info('Test log')
  63. logger.warn('Warning')
  64. try:
  65. 1/0
  66. except:
  67. logger.exception("Supporting exceptions too!")
  68. ```
  69. #### Extra Fields
  70. In case you need to dynamic metadata to your logger, other then the constant metadata from the formatter, you can use the "extra" parameter.
  71. All key values in the dictionary passed in "extra" will be presented in Logz.io as new fields in the log you are sending.
  72. Please note, that you cannot override default fields by the python logger (i.e. lineno, thread, etc..)
  73. For example:
  74. ```
  75. logger.info('Warning', extra={'extra_key':'extra_value'})
  76. ```
  77. ## Django configuration
  78. ```
  79. LOGGING = {
  80. 'version': 1,
  81. 'disable_existing_loggers': False,
  82. 'formatters': {
  83. 'verbose': {
  84. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  85. },
  86. 'logzioFormat': {
  87. 'format': '{"additional_field": "value"}'
  88. }
  89. },
  90. 'handlers': {
  91. 'console': {
  92. 'class': 'logging.StreamHandler',
  93. 'level': 'DEBUG',
  94. 'formatter': 'verbose'
  95. },
  96. 'logzio': {
  97. 'class': 'logzio.handler.LogzioHandler',
  98. 'level': 'INFO',
  99. 'formatter': 'logzioFormat',
  100. 'token': 'token',
  101. 'logzio_type': "django",
  102. 'logs_drain_timeout': 5,
  103. 'url': 'https://listener.logz.io:8071',
  104. 'debug': True
  105. },
  106. },
  107. 'loggers': {
  108. 'django': {
  109. 'handlers': ['console', ],
  110. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO')
  111. },
  112. 'appname': {
  113. 'handlers': ['console', 'logzio'],
  114. 'level': 'INFO'
  115. }
  116. }
  117. }
  118. ```
  119. *Change*
  120. - token - Your logzio token
  121. - url - Logz.io Listener address
  122. - logs_drain_count - Number of logs to keep in buffer before draining
  123. - logs_drain_timeout - Time to wait before draining, regardless of the previouse setting
  124. - logzio_type - Log type, for searching in logz.io (defaults to "python"), it cannot contain a space.
  125. - appname - Your django app
  126. ## Release Notes
  127. - 2.0.11 - Completely isolate exception from the message
  128. - 2.0.10 - Not ignoring formatting on exceptions
  129. - 2.0.9 - Support extra fields on exceptions too (Thanks @asafc64!)
  130. - 2.0.8 - Various PEP8, testings and logging changes (Thanks @nir0s!)
  131. - 2.0.7 - Make sure sending thread is alive after fork (Thanks @jo-tham!)
  132. - 2.0.6 - Add "flush()" method to manually drain the queue (Thanks @orenmazor!)
  133. - 2.0.5 - Support for extra fields
  134. - 2.0.4 - Publish package as source along wheel, and supprt python3 packagin (Thanks @cchristous!)
  135. - 2.0.3 - Fix bug that consumed more logs while draining than Logz.io's bulk limit
  136. - 2.0.2 - Support for formatted messages (Thanks @johnraz!)
  137. - 2.0.1 - Added __all__ to __init__.py, so support * imports
  138. - 2.0.0 - Production, stable release.
  139. - *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.
  140. - 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.
  141. - Simplifications and Robustness
  142. - Full testing framework
  143. - 1.X - Beta versions