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.

80 lines
2.5 KiB

  1. import fnmatch
  2. import logging.config
  3. import os
  4. import time
  5. import json
  6. from unittest import TestCase
  7. from .mockLogzioListener import listener
  8. def _find(pattern, path):
  9. result = []
  10. for root, dirs, files in os.walk(path):
  11. for name in files:
  12. if fnmatch.fnmatch(name, pattern):
  13. result.append(os.path.join(root, name))
  14. break # Not descending recursively
  15. return result
  16. class TestAddContext(TestCase):
  17. def setUp(self):
  18. self.logzio_listener = listener.MockLogzioListener()
  19. self.logzio_listener.clear_logs_buffer()
  20. self.logzio_listener.clear_server_error()
  21. self.logs_drain_timeout = 1
  22. self.retries_no = 4
  23. self.retry_timeout = 2
  24. logging_configuration = {
  25. "version": 1,
  26. "formatters": {
  27. "logzio": {
  28. "format": '{"key": "value"}',
  29. "validate": False
  30. }
  31. },
  32. "handlers": {
  33. "LogzioHandler": {
  34. "class": "logzio.handler.LogzioHandler",
  35. "formatter": "logzio",
  36. "level": "DEBUG",
  37. "token": "token",
  38. 'logzio_type': "type",
  39. 'logs_drain_timeout': self.logs_drain_timeout,
  40. 'url': "http://" + self.logzio_listener.get_host() + ":" + str(self.logzio_listener.get_port()),
  41. 'debug': True,
  42. 'retries_no': self.retries_no,
  43. 'retry_timeout': self.retry_timeout,
  44. 'add_context': True
  45. }
  46. },
  47. "loggers": {
  48. "test": {
  49. "handlers": ["LogzioHandler"],
  50. "level": "DEBUG"
  51. }
  52. }
  53. }
  54. logging.config.dictConfig(logging_configuration)
  55. self.logger = logging.getLogger('test')
  56. for curr_file in _find("logzio-failures-*.txt", "."):
  57. os.remove(curr_file)
  58. def test_add_context(self):
  59. log_message = "this log should have a trace context"
  60. self.logger.info(log_message)
  61. time.sleep(self.logs_drain_timeout * 2)
  62. logs_list = self.logzio_listener.logs_list
  63. for current_log in logs_list:
  64. if log_message in current_log:
  65. log_dict = json.loads(current_log)
  66. self.assertTrue('otelSpanID' in log_dict)
  67. self.assertTrue('otelTraceID' in log_dict)
  68. self.assertTrue('otelServiceName' in log_dict)