From 9d2015160042c038aa0f1e3c8752fd7e379e6690 Mon Sep 17 00:00:00 2001 From: kitrady Date: Wed, 31 Jul 2024 11:53:24 -0500 Subject: [PATCH] making format analyzer not flag stack traces --- src/alogamous/format_analyzer.py | 8 +++++- tests/format_analyzer_test.py | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/alogamous/format_analyzer.py b/src/alogamous/format_analyzer.py index ed99a8b..3a5753f 100644 --- a/src/alogamous/format_analyzer.py +++ b/src/alogamous/format_analyzer.py @@ -5,11 +5,17 @@ class FormatAnalyzer(analyzer.Analyzer): def __init__(self, line_parser): self.parser = line_parser self.startup_block = False + self.stack_trace = 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.count("Traceback") == 1: + self.stack_trace = True + elif self.stack_trace: + if line_type == (log_line_parser.LineType.LOG_LINE or log_line_parser.LineType.HEADER_LINE): + self.stack_trace = False + elif self.startup_block is False and self.stack_trace 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: diff --git a/tests/format_analyzer_test.py b/tests/format_analyzer_test.py index 85d9e66..f8c11f3 100644 --- a/tests/format_analyzer_test.py +++ b/tests/format_analyzer_test.py @@ -53,3 +53,52 @@ def test_format_analyzer_with_good_lines(): format_checker.read_log_line(line) format_checker.report(out_stream) assert out_stream.getvalue() == "All lines conform to log line format" + + +def test_format_with_stack_trace(): + parser = log_line_parser.LogLineParser( + ["datetime", "source", "level", "message"], " - ", "====================================================" + ) + format_checker = format_analyzer.FormatAnalyzer(parser) + in_stream = """2024-07-23 21:13:53,862 - root - INFO - Closing client connection. +Hello I am a bad log line +2024-07-23 21:13:53,862 - root - INFO - Closing client connection. +Traceback (most recent call last): + File "", line 1938, in _run_module_as_main + File "", line 88, in _run_code + File ".build/2811/execution/execution_service", line 973, in + app.run(life_cycle_runner.run, life_cycle_runner.stop) + File ".build/2811/app/application.py", line 219, in run + run(self.start(my_date, main, stop)) + File ".build/2811/.venv/lib/python3.11/runners.py", line 190, in run + return runner.run(main) + ^^^^^^^^^^^^^^^^ + File ".build/2811/.venv/lib/python3.11/runners.py", line 118, in run + return self._loop.run_until_complete(task) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File ".build/2811/.venv/lib/python3.11/events.py", line 653, in run_until_done + return future.result() + ^^^^^^^^^^^^^^^ + File "2811/app/application.py", line 421, in start + await self.task + File "2811/messages/app/runner.py", line 449, in run + await asyncio.gather(*self.running_tasks) + File "2811/messages/processor.py", line 340, in run + await self.dispatch(message) + File ".build/2811/execution/execution_service", line 1315, in market_test + if symbol and obj.region != 'NORTHAMERICA' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +AttributeError: 'NoneType' object has no attribute 'region' +2024-07-23 21:13:53,862 - root - INFO - Closing client connection. +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""" + )