Skip to content

Commit

Permalink
Parse Startup Message (#52)
Browse files Browse the repository at this point in the history
* added parser that turns log line strings into dictionaries

* adding constant strings to avoid typo errors

* adding analyzer to parse startup header

---------

Co-authored-by: krady <[email protected]>
  • Loading branch information
kitrady and krady authored Jul 19, 2024
1 parent 6a39a70 commit e94158d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/alogamous/startup_header_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from alogamous import analyzer, log_line_parser

parser = log_line_parser.LogLineParser(
"====================================================", ["datetime", "source", "level", "message"], " - "
)


class StartupHeaderAnalyzer(analyzer.Analyzer):
def __init__(self):
self.startup_block = False
self.startup_lines = []

def read_log_line(self, line):
line_type = parser.parse(line)["type"]
if self.startup_block is False and line_type == log_line_parser.LineType.HEADER_LINE:
self.startup_block = True
elif self.startup_block is True:
if line_type == log_line_parser.LineType.UNSTRUCTURED_LINE:
self.startup_lines.append(line)
elif line_type == log_line_parser.LineType.HEADER_LINE:
self.startup_block = False

def report(self, out_stream):
out_stream.write("\n".join(self.startup_lines))
29 changes: 29 additions & 0 deletions tests/startup_header_analyzer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import io

from alogamous import startup_header_analyzer


def test_report():
startup_analyzer = startup_header_analyzer.StartupHeaderAnalyzer()
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
"""
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']"""
)

0 comments on commit e94158d

Please sign in to comment.