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.

176 lines
5.1 KiB

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