Skip to content

Commit

Permalink
use regex for timestamp localization, suppress hostname in logs in sy…
Browse files Browse the repository at this point in the history
…stemd
  • Loading branch information
Roy Wiggins authored and Roy Wiggins committed Oct 3, 2024
1 parent 7b9f43a commit 928616a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions common/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import time as _time
import inspect
from pathlib import Path
import re
import threading
from typing import Callable, List, Optional, Tuple
import dateutil
Expand Down Expand Up @@ -37,23 +38,28 @@ def validate_folders(config) -> Tuple[bool, str]:
return False, f"No read/write access to {folder}"
return True, ""


def localize_log_timestamps(loglines: List[str], config) -> None:
if config.mercure.local_time == "UTC":
return

try:
local_tz: datetime.tzinfo = dateutil.tz.gettz(config.mercure.local_time)
except:
return

timestamp_pattern = re.compile(r'^(\S+)')

for i, line in enumerate(loglines):
split = line.split(" ")
timestamp = split[0]
match = timestamp_pattern.match(line)
if not match:
continue

timestamp = match.group(1)
try:
parsed_dt = dateutil.parser.isoparse(timestamp)
dt_localtime:datetime.datetime = parsed_dt.astimezone(local_tz)
split[0] = dt_localtime.isoformat(timespec='seconds')
loglines[i] = " ".join(split)
dt_localtime: datetime.datetime = parsed_dt.astimezone(local_tz)
localized_timestamp = dt_localtime.isoformat(timespec='seconds')
loglines[i] = timestamp_pattern.sub(localized_timestamp, line)
except:
pass

Expand Down
2 changes: 1 addition & 1 deletion webgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async def show_log(request) -> Response:
f"sudo journalctl -n 1000 -u "
f'{service_name} '
f"{start_date_cmd} {end_date_cmd} "
"-o short-iso"
"-o short-iso --no-hostname"
)
return_code = -1 if run_result[0] is None else run_result[0]
raw_logs = run_result[1]
Expand Down

0 comments on commit 928616a

Please sign in to comment.