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.

153 lines
5.5 KiB

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