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.

116 lines
3.4 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_exc(self):
  68. try:
  69. raise ValueError("oops.")
  70. except:
  71. exc_info = sys.exc_info()
  72. record = logging.LogRecord(
  73. name='my-logger',
  74. level=0,
  75. pathname='handler_test.py',
  76. lineno=10,
  77. msg="this is a test: moo.",
  78. args=[],
  79. exc_info=exc_info,
  80. func='test_json'
  81. )
  82. formatted_message = self.handler.format_message(record)
  83. formatted_message["@timestamp"] = None
  84. formatted_message["exception"] = formatted_message["exception"].replace(os.path.abspath(__file__), "")
  85. formatted_message["exception"] = re.sub(r", line \d+", "", formatted_message["exception"])
  86. self.assertDictEqual(
  87. {
  88. '@timestamp': None,
  89. 'line_number': 10,
  90. 'log_level': 'NOTSET',
  91. 'logger': 'my-logger',
  92. 'message': 'this is a test: moo.',
  93. 'exception': 'Traceback (most recent call last):\n\n File "", in test_exc\n raise ValueError("oops.")\n\nValueError: oops.\n',
  94. 'path_name': 'handler_test.py',
  95. 'type': 'python'
  96. },
  97. formatted_message
  98. )