Skip to content

Commit 7b7a4c1

Browse files
authored
#490: In CommandLogHandler suffixed newline, flushed logfile and added timestamp (#499)
Added timestamp & newline for each invocation of handle_log_line() and flushed the log file. Added unit tests for timestamp & newline.
1 parent 7ca994a commit 7b7a4c1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

exasol_integration_test_docker_environment/lib/logging/command_log_handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import pathlib
2+
from datetime import (
3+
datetime,
4+
timezone,
5+
)
26

37
from exasol_integration_test_docker_environment.lib.logging.abstract_log_handler import (
48
AbstractLogHandler,
@@ -15,8 +19,11 @@ def __init__(self, log_file_path: pathlib.Path, logger, description: str):
1519
self._description = description
1620

1721
def handle_log_line(self, log_line, error: bool = False):
18-
self._log_file.write(log_line)
19-
self._complete_log.append(log_line)
22+
cur_time = datetime.now(timezone.utc)
23+
log_time = cur_time.strftime("%H.%M.%S.%f")
24+
self._log_file.write(f"{log_time}: {log_line}\n")
25+
self._log_file.flush()
26+
self._complete_log.append(f"{log_time}: {log_line}\n")
2027
if error:
2128
self._error_message = log_line
2229

test/unit/test_log_newline.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import platform
3+
from pathlib import Path
4+
from test.matchers import regex_matcher
5+
6+
import pytest
7+
8+
from exasol_integration_test_docker_environment.lib.logging.command_log_handler import (
9+
CommandLogHandler,
10+
)
11+
12+
13+
def test_new_line(tmp_path):
14+
log_file_path = Path(tmp_path, "test_cmd_log_handler.log")
15+
with CommandLogHandler(log_file_path, None, "test for multi lines") as log_handler:
16+
log_handler.handle_log_line("log line 1")
17+
log_handler.handle_log_line("log line 2")
18+
with open(log_file_path) as log_file:
19+
line_count = sum(1 for line in log_file)
20+
assert line_count > 1
21+
22+
23+
def test_timestamp(tmp_path):
24+
log_file_path = Path(tmp_path, "test_cmd_log_handler.log")
25+
with CommandLogHandler(log_file_path, None, "test for multi lines") as log_handler:
26+
log_handler.handle_log_line("log line 1")
27+
with open(log_file_path) as log_file:
28+
first_line = log_file.readline()
29+
# pattern is, 0 padded hh.mm.ss.6-digits-usecs
30+
assert first_line == regex_matcher(r"^\d{2}.\d{2}.\d{2}\.\d{6}")

0 commit comments

Comments
 (0)