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.

135 lines
4.5 KiB

8 years ago
9 years ago
8 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. ## Django configuration
  63. ```
  64. LOGGING = {
  65. 'version': 1,
  66. 'disable_existing_loggers': False,
  67. 'formatters': {
  68. 'verbose': {
  69. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  70. },
  71. 'logzioFormat': {
  72. 'format': '{"additional_field": "value"}'
  73. }
  74. },
  75. 'handlers': {
  76. 'console': {
  77. 'class': 'logging.StreamHandler',
  78. 'level': 'DEBUG',
  79. 'formatter': 'verbose'
  80. },
  81. 'logzio': {
  82. 'class': 'logzio.handler.LogzioHandler',
  83. 'level': 'INFO',
  84. 'formatter': 'logzioFormat',
  85. 'token': 'token',
  86. 'logzio_type': "django",
  87. 'logs_drain_timeout': 5,
  88. 'url': 'https://listener.logz.io:8071',
  89. 'debug': True
  90. },
  91. },
  92. 'loggers': {
  93. 'django': {
  94. 'handlers': ['console', ],
  95. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO')
  96. },
  97. 'appname': {
  98. 'handlers': ['console', 'logzio'],
  99. 'level': 'INFO'
  100. }
  101. }
  102. }
  103. ```
  104. *Change*
  105. - token - Your logzio token
  106. - url - Logz.io Listener address
  107. - logs_drain_count - Number of logs to keep in buffer before draining
  108. - logs_drain_timeout - Time to wait before draining, regardless of the previouse setting
  109. - logzio_type - Log type, for searching in logz.io (defaults to "python")
  110. - appname - Your django app
  111. ## Release Notes
  112. - 2.0.0 - Production, stable release.
  113. - *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.
  114. - 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.
  115. - Simplifications and Robustness
  116. - Full testing framework
  117. - 1.X - Beta versions