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.

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