Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding and debugging format analyzer #64

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/alogamous/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
directory_reader,
error_counter_analyzer,
flag_duplicate_log_messages,
format_analyzer,
line_count_analyzer,
log_line_parser,
loginfo_analyzer,
Expand Down Expand Up @@ -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(),
Expand Down
22 changes: 22 additions & 0 deletions src/alogamous/format_analyzer.py
Original file line number Diff line number Diff line change
@@ -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))
3 changes: 2 additions & 1 deletion src/alogamous/startup_header_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
33 changes: 33 additions & 0 deletions tests/format_analyzer_test.py
Original file line number Diff line number Diff line change
@@ -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"""
)
16 changes: 8 additions & 8 deletions tests/startup_header_analyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ 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
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
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']"""
)
Loading