From 445dbcfc3177a3c8c24853d1864af38e90fdb346 Mon Sep 17 00:00:00 2001 From: kitrady Date: Sat, 20 Jul 2024 15:29:44 -0500 Subject: [PATCH] adding and debugging format analyzer --- src/alogamous/__main__.py | 2 ++ src/alogamous/format_analyzer.py | 22 ++++++++++++++++ src/alogamous/startup_header_analyzer.py | 3 ++- tests/format_analyzer_test.py | 33 ++++++++++++++++++++++++ tests/startup_header_analyzer_test.py | 18 ++++++------- 5 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 src/alogamous/format_analyzer.py create mode 100644 tests/format_analyzer_test.py diff --git a/src/alogamous/__main__.py b/src/alogamous/__main__.py index 2c51e4b..99cea5d 100644 --- a/src/alogamous/__main__.py +++ b/src/alogamous/__main__.py @@ -5,6 +5,7 @@ directory_reader, echo_analyzer, error_counter_analyzer, + format_analyzer, line_count_analyzer, log_line_parser, loginfo_analyzer, @@ -27,6 +28,7 @@ echo_analyzer.EchoAnalyzer(), error_counter_analyzer.ErrorCounterAnalyzer(), line_count_analyzer.LineCountAnalyzer(), + format_analyzer.FormatAnalyzer(line_parser), warning_analyzer.WarningAnalyzer(), loginfo_analyzer.InfoAnalyzer(), ], diff --git a/src/alogamous/format_analyzer.py b/src/alogamous/format_analyzer.py new file mode 100644 index 0000000..1b70b66 --- /dev/null +++ b/src/alogamous/format_analyzer.py @@ -0,0 +1,22 @@ +from alogamous import analyzer, log_line_parser + + +class FormatAnalyzer(analyzer.Analyzer): + def __init__(self, line_parser): + self.parser = line_parser + self.startup_block = False + self.un_formated_lines = [] + + def read_log_line(self, line): + line_type = self.parser.parse(line)["type"] + if self.startup_block is False: + if line_type == log_line_parser.LineType.UNSTRUCTURED_LINE: + self.un_formated_lines.append(line) + elif line_type == log_line_parser.LineType.HEADER_LINE: + self.startup_block = True + elif self.startup_block is True and line_type == log_line_parser.LineType.HEADER_LINE: + self.startup_block = False + + def report(self, out_stream): + out_stream.write("\nLines that do not conform to log format:\n- ") + out_stream.write("\n- ".join(self.un_formated_lines)) diff --git a/src/alogamous/startup_header_analyzer.py b/src/alogamous/startup_header_analyzer.py index 7d7748c..957af8c 100644 --- a/src/alogamous/startup_header_analyzer.py +++ b/src/alogamous/startup_header_analyzer.py @@ -21,4 +21,5 @@ def read_log_line(self, line): self.startup_block = False def report(self, out_stream): - out_stream.write("\n".join(self.startup_lines)) + out_stream.write("\nLines that are part of the startup header:\n- ") + out_stream.write("\n- ".join(self.startup_lines)) diff --git a/tests/format_analyzer_test.py b/tests/format_analyzer_test.py new file mode 100644 index 0000000..ceb9f40 --- /dev/null +++ b/tests/format_analyzer_test.py @@ -0,0 +1,33 @@ +from io import StringIO + +from alogamous import format_analyzer, log_line_parser + + +def test_format_analyzer(): + parser = log_line_parser.LogLineParser( + ["datetime", "source", "level", "message"], " - ", "====================================================" + ) + format_checker = format_analyzer.FormatAnalyzer(parser) + in_stream = """==================================================== +STARTING Tracking service + Start time: 2024-06-20 09:00:00.001550+00:00 + Version: 2729a + Command line: ['.venv/bin/python3', '-m', 'app.tracking_service', '--market', 'US', '--version', '2729a'] +==================================================== +2024-06-20 11:00:17,983 - root - INFO - Adding subscription for pid None +Hello I am a bad log line +2024-06-20 11:00:18,115 - root - INFO - Initialized Influx DB Client to host +2024-06-20 11:00:18,115 - root - INFO - Scheduling Error Handler in 150.0 seconds +2024-06-20 11:00:18,116 - root - INFO - prometheus client http server running +Hello I am a bad log line""" + out_stream = StringIO() + for line in in_stream.splitlines(): + format_checker.read_log_line(line) + format_checker.report(out_stream) + assert ( + out_stream.getvalue() + == """ +Lines that do not conform to log format: +- Hello I am a bad log line +- Hello I am a bad log line""" + ) diff --git a/tests/startup_header_analyzer_test.py b/tests/startup_header_analyzer_test.py index 5608daa..470ee14 100644 --- a/tests/startup_header_analyzer_test.py +++ b/tests/startup_header_analyzer_test.py @@ -3,10 +3,9 @@ from alogamous import startup_header_analyzer -def test_report(): +def test_startup_header_analyzer(): startup_analyzer = startup_header_analyzer.StartupHeaderAnalyzer() - in_stream = """ -==================================================== + in_stream = """==================================================== STARTING Tracking service Start time: 2024-06-20 09:00:00.001550+00:00 Version: 2729a @@ -14,16 +13,17 @@ def test_report(): ==================================================== 2024-06-20 11:00:17,983 - root - INFO - Adding subscription for pid None 2024-06-20 11:00:18,115 - root - INFO - Initialized Influx DB Client to host -2024-06-20 11:00:18,115 - root - INFO - Scheduling Error Handler in 150.0 seconds -""" +2024-06-20 11:00:18,115 - root - INFO - Scheduling Error Handler in 150.0 seconds""" out_stream = io.StringIO() for line in in_stream.splitlines(): startup_analyzer.read_log_line(line) startup_analyzer.report(out_stream) assert ( out_stream.getvalue() - == """STARTING Tracking service - Start time: 2024-06-20 09:00:00.001550+00:00 - Version: 2729a - Command line: ['.venv/bin/python3', '-m', 'app.tracking_service', '--market', 'US', '--version', '2729a']""" + == """ +Lines that are part of the startup header: +- STARTING Tracking service +- Start time: 2024-06-20 09:00:00.001550+00:00 +- Version: 2729a +- Command line: ['.venv/bin/python3', '-m', 'app.tracking_service', '--market', 'US', '--version', '2729a']""" )