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.

134 lines
4.8 KiB

  1. import fnmatch
  2. import logging.config
  3. import os
  4. import time
  5. import json
  6. from unittest import TestCase
  7. from logzio.handler import ExtraFieldsLogFilter
  8. from .mockLogzioListener import listener
  9. def _find(pattern, path):
  10. result = []
  11. for root, dirs, files in os.walk(path):
  12. for name in files:
  13. if fnmatch.fnmatch(name, pattern):
  14. result.append(os.path.join(root, name))
  15. break # Not descending recursively
  16. return result
  17. class TestExtraFieldsFilter(TestCase):
  18. def setUp(self):
  19. self.logzio_listener = listener.MockLogzioListener()
  20. self.logzio_listener.clear_logs_buffer()
  21. self.logzio_listener.clear_server_error()
  22. self.logs_drain_timeout = 1
  23. self.retries_no = 4
  24. self.retry_timeout = 2
  25. self.add_context = True
  26. logging_configuration = {
  27. "version": 1,
  28. "formatters": {
  29. "logzio": {
  30. "format": '{"key": "value"}',
  31. "validate": False
  32. }
  33. },
  34. "handlers": {
  35. "LogzioHandler": {
  36. "class": "logzio.handler.LogzioHandler",
  37. "formatter": "logzio",
  38. "level": "DEBUG",
  39. "token": "token",
  40. 'logzio_type': "type",
  41. 'logs_drain_timeout': self.logs_drain_timeout,
  42. 'url': "http://" + self.logzio_listener.get_host() + ":" + str(self.logzio_listener.get_port()),
  43. 'debug': True,
  44. 'retries_no': self.retries_no,
  45. 'retry_timeout': self.retry_timeout,
  46. 'add_context': self.add_context
  47. }
  48. },
  49. "loggers": {
  50. "test": {
  51. "handlers": ["LogzioHandler"],
  52. "level": "DEBUG"
  53. }
  54. }
  55. }
  56. logging.config.dictConfig(logging_configuration)
  57. self.logger = logging.getLogger('test')
  58. for curr_file in _find("logzio-failures-*.txt", "."):
  59. os.remove(curr_file)
  60. def test_add_extra_fields(self):
  61. extra_fields = {"foo": "bar"}
  62. self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields))
  63. log_message = "this log should have a additional fields"
  64. self.logger.info(log_message)
  65. time.sleep(self.logs_drain_timeout * 2)
  66. logs_list = self.logzio_listener.logs_list
  67. for current_log in logs_list:
  68. if log_message in current_log:
  69. log_dict = json.loads(current_log)
  70. try:
  71. self.assertEqual(extra_fields, {**extra_fields, **log_dict})
  72. except AssertionError as err:
  73. print(err)
  74. def test_remove_extra_fields(self):
  75. extra_fields = {"foo": "bar"}
  76. self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields))
  77. log_message = "this log should have a additional fields"
  78. self.logger.info(log_message)
  79. self.logger.removeFilter(ExtraFieldsLogFilter(extra=extra_fields))
  80. unfiltered_log_message = "this log shouldn't have a additional fields"
  81. self.logger.info(unfiltered_log_message)
  82. time.sleep(self.logs_drain_timeout * 2)
  83. logs_list = self.logzio_listener.logs_list
  84. for current_log in logs_list:
  85. if unfiltered_log_message in current_log:
  86. log_dict = json.loads(current_log)
  87. try:
  88. self.assertNotEqual(extra_fields, {**extra_fields, **log_dict})
  89. except AssertionError as err:
  90. print(err)
  91. def test_add_multiple_extra_fields(self):
  92. extra_fields = {"foo": "bar"}
  93. self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields))
  94. log_message = "this log should have additional fields"
  95. self.logger.info(log_message)
  96. extra_fields = {"counter":1}
  97. self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields))
  98. filtered_log_message = "this log should have multiple additional fields"
  99. self.logger.info(filtered_log_message)
  100. time.sleep(self.logs_drain_timeout * 2)
  101. logs_list = self.logzio_listener.logs_list
  102. for current_log in logs_list:
  103. if log_message in current_log:
  104. log_dict = json.loads(current_log)
  105. try:
  106. self.assertEqual(extra_fields, {**extra_fields, **log_dict})
  107. except AssertionError as err:
  108. print(err)
  109. elif filtered_log_message in current_log:
  110. log_dict = json.loads(current_log)
  111. try:
  112. self.assertEqual(extra_fields, {**extra_fields, **log_dict})
  113. except AssertionError as err:
  114. print(err)
  115. if __name__ == '__main__':
  116. unittest.main()