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.

124 lines
4.2 KiB

8 years ago
9 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)
  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. ## Python configuration
  14. #### Config File
  15. ```
  16. [handlers]
  17. keys=LogzioHandler
  18. [handler_LogzioHandler]
  19. class=logzio.handler.LogzioHandler
  20. formatter=logzioFormat
  21. args=('token', 'my_type')
  22. [formatters]
  23. keys=logzioFormat
  24. [loggers]
  25. keys=root
  26. [logger_root]
  27. handlers=LogzioHandler
  28. level=INFO
  29. [formatter_logzioFormat]
  30. format={"additional_field": "value"}
  31. ```
  32. *args=() arguments, by order*
  33. - Your logz.io token
  34. - Log type, for searching in logz.io (defaults to "python")
  35. - Time to sleep between draining attempts (defaults to "3")
  36. - Logz.io Listener address (defaults to "https://listener.logz.io:8071")
  37. - Debug flag. Set to True, will print debug messages to stdout. (defaults to "False")
  38. Please note, that you have to configure those parameters by this exact order.
  39. i.e. you cannot set Debug to true, without configuring all of the previous parameters as well.
  40. #### Code Example
  41. ```python
  42. import logging
  43. import logging.config
  44. # Say i have saved my configuration under ./myconf.conf
  45. logging.config.fileConfig('myconf.conf')
  46. logger = logging.getLogger('superAwesomeLogzioLogger')
  47. logger.info('Test log')
  48. logger.warn('Warning')
  49. try:
  50. 1/0
  51. except:
  52. logger.exception("Supporting exceptions too!")
  53. ```
  54. ## Django configuration
  55. ```
  56. LOGGING = {
  57. 'version': 1,
  58. 'disable_existing_loggers': False,
  59. 'formatters': {
  60. 'verbose': {
  61. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  62. },
  63. 'logzioFormat': {
  64. 'format': '{"additional_field": "value"}'
  65. }
  66. },
  67. 'handlers': {
  68. 'console': {
  69. 'class': 'logging.StreamHandler',
  70. 'level': 'DEBUG',
  71. 'formatter': 'verbose'
  72. },
  73. 'logzio': {
  74. 'class': 'logzio.handler.LogzioHandler',
  75. 'level': 'INFO',
  76. 'formatter': 'logzioFormat',
  77. 'token': 'token',
  78. 'logzio_type': "django",
  79. 'logs_drain_timeout': 5,
  80. 'url': 'https://listener.logz.io:8071',
  81. 'debug': True
  82. },
  83. },
  84. 'loggers': {
  85. 'django': {
  86. 'handlers': ['console', ],
  87. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO')
  88. },
  89. 'appname': {
  90. 'handlers': ['console', 'logzio'],
  91. 'level': 'INFO'
  92. }
  93. }
  94. }
  95. ```
  96. *Change*
  97. - token - Your logzio token
  98. - url - Logz.io Listener address
  99. - logs_drain_count - Number of logs to keep in buffer before draining
  100. - logs_drain_timeout - Time to wait before draining, regardless of the previouse setting
  101. - logzio_type - Log type, for searching in logz.io (defaults to "python")
  102. - appname - Your django app
  103. ## Release Notes
  104. - 2.0.0 - Production, stable release.
  105. - *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.
  106. - 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.
  107. - Simplifications and Robustness
  108. - Full testing framework
  109. - 1.X - Beta versions