Add timestamp to logs (#3245)

This commit is contained in:
Daniel Girtler 2025-03-12 18:37:13 +11:00 committed by GitHub
parent 7f88e909c6
commit e46aa7c1b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import sys
import unicodedata
from collections.abc import Callable
from dataclasses import asdict, is_dataclass
from datetime import UTC, datetime
from enum import Enum
from functools import lru_cache
from pathlib import Path
@ -264,6 +265,11 @@ def info(
log(*msgs, level=level, fg=fg, bg=bg, reset=reset, font=font)
def _timestamp() -> str:
now = datetime.now(tz=UTC)
return now.strftime('%Y-%m-%d %H:%M:%S')
def debug(
*msgs: str,
level: int = logging.DEBUG,
@ -319,7 +325,10 @@ def log(
log_file: Path = storage['LOG_PATH'] / storage['LOG_FILE']
with log_file.open('a') as fp:
fp.write(f"{orig_string}\n")
ts = _timestamp()
level_name = logging.getLevelName(level)
out = f"[{ts}] - {level_name} - {orig_string}\n"
fp.write(out)
Journald.log(text, level=level)