log: stamp mirrored stdout/stderr lines with <date>T<time>
Tee (server/brewpi.py) mirrors print() output to logs/brewpi.*.log but previously wrote it verbatim, so those files had no timestamps to correlate against the JSON sample logs. Prefix each line instead.
This commit is contained in:
+20
-2
@@ -25,14 +25,32 @@ class Tee:
|
||||
"""Duplicates writes to multiple streams - used to mirror stdout/stderr
|
||||
(everything the rest of the codebase already reaches via print()) into
|
||||
a log file under logs/ as well as the console, without having to touch
|
||||
every print() call site."""
|
||||
every print() call site.
|
||||
|
||||
Also stamps every line with a "<date>T<time>:" prefix (existing print()
|
||||
call sites already start their message with "Component: ..." - see e.g.
|
||||
tasks/stirrer.py - so the combined line reads
|
||||
"<date>T<time>:<component>: <message>"), so log entries can be
|
||||
correlated with the JSON sample logs (ServerLogTask/SudLogTask) without
|
||||
relying on journalctl's own timestamps, which aren't present in the
|
||||
mirrored log files themselves."""
|
||||
|
||||
def __init__(self, *streams):
|
||||
self.streams = streams
|
||||
self._at_line_start = True
|
||||
|
||||
def write(self, data):
|
||||
if not data:
|
||||
return
|
||||
out = []
|
||||
for line in data.splitlines(keepends=True):
|
||||
if self._at_line_start:
|
||||
out.append(time.strftime("%Y-%m-%dT%H:%M:%S:", time.localtime()))
|
||||
out.append(line)
|
||||
self._at_line_start = line.endswith('\n')
|
||||
text = ''.join(out)
|
||||
for stream in self.streams:
|
||||
stream.write(data)
|
||||
stream.write(text)
|
||||
|
||||
def flush(self):
|
||||
for stream in self.streams:
|
||||
|
||||
Reference in New Issue
Block a user