diff --git a/src/alogamous/__main__.py b/src/alogamous/__main__.py index c6ce513..bae0f67 100644 --- a/src/alogamous/__main__.py +++ b/src/alogamous/__main__.py @@ -5,6 +5,7 @@ directory_reader, error_counter_analyzer, flag_duplicate_log_messages, + format_analyzer, line_count_analyzer, log_line_parser, loginfo_analyzer, @@ -37,6 +38,8 @@ error_counter_analyzer.ErrorCounterAnalyzer(), flag_duplicate_log_messages.FlagDuplicateLogMessages(), line_count_analyzer.LineCountAnalyzer(), + format_analyzer.FormatAnalyzer(line_parser), + warning_analyzer.WarningAnalyzer(), loginfo_analyzer.InfoAnalyzer(line_parser), startup_header_analyzer.StartupHeaderAnalyzer(line_parser), warning_analyzer.WarningAnalyzer(), 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 abc6177..7036124 100644 --- a/src/alogamous/startup_header_analyzer.py +++ b/src/alogamous/startup_header_analyzer.py @@ -18,4 +18,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 8fb54de..f2468f0 100644 --- a/tests/startup_header_analyzer_test.py +++ b/tests/startup_header_analyzer_test.py @@ -8,8 +8,7 @@ def test_report(): ["datetime", "source", "level", "message"], " - ", "====================================================" ) startup_analyzer = startup_header_analyzer.StartupHeaderAnalyzer(line_parser) - in_stream = """ -==================================================== + in_stream = """==================================================== STARTING Tracking service Start time: 2024-06-20 09:00:00.001550+00:00 Version: 2729a @@ -17,16 +16,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']""" )