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.

175 lines
5.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import os
  2. from unittest import TestCase
  3. import logging
  4. import sys
  5. import re
  6. from logzio.handler import LogzioHandler
  7. class TestLogzioHandler(TestCase):
  8. def setUp(self):
  9. self.handler = LogzioHandler('moo')
  10. def test_json(self):
  11. formatter = logging.Formatter(
  12. '{ "appname":"%(name)s", "functionName":"%(funcName)s", \"lineNo":"%(lineno)d", "severity":"%('
  13. 'levelname)s", "message":"%(message)s"}')
  14. self.handler.setFormatter(formatter)
  15. record = logging.LogRecord(
  16. name='my-logger',
  17. level=0,
  18. pathname='handler_test.py',
  19. lineno=10,
  20. msg="this is a test: moo.",
  21. args=[],
  22. exc_info=None,
  23. func='test_json'
  24. )
  25. formatted_message = self.handler.format_message(record)
  26. formatted_message["@timestamp"] = None
  27. self.assertDictEqual(
  28. formatted_message,
  29. {
  30. '@timestamp': None,
  31. 'appname': 'my-logger',
  32. 'functionName': 'test_json',
  33. 'lineNo': '10',
  34. 'line_number': 10,
  35. 'log_level': 'NOTSET',
  36. 'logger': 'my-logger',
  37. 'message': 'this is a test: moo.',
  38. 'path_name': 'handler_test.py',
  39. 'severity': 'NOTSET',
  40. 'type': 'python'
  41. }
  42. )
  43. def test_string(self):
  44. record = logging.LogRecord(
  45. name='my-logger',
  46. level=0,
  47. pathname='handler_test.py',
  48. lineno=10,
  49. msg="this is a test: moo.",
  50. args=[],
  51. exc_info=None,
  52. func='test_json'
  53. )
  54. formatted_message = self.handler.format_message(record)
  55. formatted_message["@timestamp"] = None
  56. self.assertDictEqual(
  57. formatted_message,
  58. {
  59. '@timestamp': None,
  60. 'line_number': 10,
  61. 'log_level': 'NOTSET',
  62. 'logger': 'my-logger',
  63. 'message': 'this is a test: moo.',
  64. 'path_name': 'handler_test.py',
  65. 'type': 'python'
  66. }
  67. )
  68. def test_extra_formatting(self):
  69. record = logging.LogRecord(
  70. name='my-logger',
  71. level=0,
  72. pathname='handler_test.py',
  73. lineno=10,
  74. msg="this is a test: moo.",
  75. args=[],
  76. exc_info=None,
  77. func='test_json'
  78. )
  79. record.__dict__["extra_key"] = "extra_value"
  80. record.__dict__["module"] = "testing"
  81. formatted_message = self.handler.format_message(record)
  82. formatted_message["@timestamp"] = None
  83. self.assertDictEqual(
  84. formatted_message,
  85. {
  86. '@timestamp': None,
  87. 'line_number': 10,
  88. 'log_level': 'NOTSET',
  89. 'logger': 'my-logger',
  90. 'message': 'this is a test: moo.',
  91. 'path_name': 'handler_test.py',
  92. 'type': 'python',
  93. 'extra_key': 'extra_value'
  94. }
  95. )
  96. def test_format_string_message(self):
  97. record = logging.LogRecord(
  98. name='my-logger',
  99. level=0,
  100. pathname='handler_test.py',
  101. lineno=10,
  102. msg="this is a test: %s.",
  103. args=('moo',),
  104. exc_info=None,
  105. func='test_json'
  106. )
  107. formatted_message = self.handler.format_message(record)
  108. formatted_message["@timestamp"] = None
  109. self.assertDictEqual(
  110. formatted_message,
  111. {
  112. '@timestamp': None,
  113. 'line_number': 10,
  114. 'log_level': 'NOTSET',
  115. 'logger': 'my-logger',
  116. 'message': 'this is a test: moo.',
  117. 'path_name': 'handler_test.py',
  118. 'type': 'python'
  119. }
  120. )
  121. def test_exc(self):
  122. try:
  123. raise ValueError("oops.")
  124. except:
  125. exc_info = sys.exc_info()
  126. record = logging.LogRecord(
  127. name='my-logger',
  128. level=0,
  129. pathname='handler_test.py',
  130. lineno=10,
  131. msg='exception test:',
  132. args=[],
  133. exc_info=exc_info,
  134. func='test_json'
  135. )
  136. formatted_message = self.handler.format_message(record)
  137. formatted_message["@timestamp"] = None
  138. formatted_message["exception"] = formatted_message["exception"].replace(os.path.abspath(__file__), "")
  139. formatted_message["exception"] = re.sub(r", line \d+", "", formatted_message["exception"])
  140. self.assertDictEqual(
  141. {
  142. '@timestamp': None,
  143. 'line_number': 10,
  144. 'log_level': 'NOTSET',
  145. 'logger': 'my-logger',
  146. 'message': 'exception test:',
  147. 'exception': 'Traceback (most recent call last):\n\n File "", in test_exc\n raise ValueError("oops.")\n\nValueError: oops.\n',
  148. 'path_name': 'handler_test.py',
  149. 'type': 'python'
  150. },
  151. formatted_message
  152. )