Skip to content

Commit bbc793c

Browse files
committed
ServiceLogger Improve output of services logs.
A better formatting, so they slightly match ASH's logs.
1 parent 50324f9 commit bbc793c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

ash/service_logging.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import threading
22

3-
from nxtools import logging
3+
from nxtools import logging, log_traceback
44

55

66
class ServiceLog:
@@ -10,16 +10,28 @@ def __init__(self, service_name: str, container):
1010
threading.Thread(target=self._run, daemon=True).start()
1111

1212
def _run(self):
13-
logging.info(f"Starting log stream for {self.service_name}")
14-
for line in self.container.logs(stream=True, tail=1, follow=True):
15-
print(f"{line.decode().strip()}")
13+
logging.info(f"Starting log stream for {self.service_name}, last 10 lines were...")
14+
for line in self.container.logs(stream=True, tail=10, stderr=True):
15+
log_string = line.decode().strip()
16+
log_elements = log_string.split(" ")
17+
18+
log_date = log_elements[0]
19+
log_time = log_elements[1]
20+
log_severity = log_elements[2]
21+
22+
log_message = log_string.split(log_severity)[-1]
23+
print(f"{log_date} {log_time} {log_severity} {self.service_name} {log_message}")
1624

1725
# service exited
1826
# print the status code and free the container
1927

20-
status_code = self.container.wait()["StatusCode"]
21-
logging.warning(f"{self.service_name} exited with code {status_code}")
22-
self.container = None
28+
try:
29+
status_code = self.container.wait()["StatusCode"]
30+
logging.warning(f"{self.service_name} exited with code {status_code}")
31+
except Exception as e:
32+
logging.warning(f"Lost connection to the container:")
33+
log_traceback(e)
34+
self.container = None
2335

2436

2537
class ServiceLogger:

0 commit comments

Comments
 (0)