FireBot/logs.py

29 lines
797 B
Python
Raw Normal View History

2023-11-05 01:37:35 +00:00
#!/usr/bin/python3
from datetime import datetime as dt
from sys import stdout, stderr
2023-11-09 03:20:50 +00:00
from typing import Union
2023-11-05 01:37:35 +00:00
def log(
2023-11-09 21:37:08 +00:00
message: str,
origin: str = "Unknown",
level: str = "LOG",
time: Union[dt, str] = "now",
2023-11-05 01:37:35 +00:00
) -> None:
if level in ["EXIT", "CRASH"]:
2023-11-05 01:37:35 +00:00
stream = stderr
else:
stream = stdout
2023-11-05 03:55:26 +00:00
if time == "now":
2023-11-13 21:42:46 +00:00
dtime = dt.now()
if type(time) == str:
raise ValueError("Only \"now\" is an accepted string argument for time")
else:
dtime = time # type: dt #type: ignore
time = dtime.strftime("%H:%M:%S")
2023-11-05 01:37:35 +00:00
if not "\n" in message:
print(f"[{level}][{origin}][{time}] {message}", file=stream)
2023-11-05 01:37:35 +00:00
else:
for line in message.split("\n"):
print(f"[{level}][{origin}][{time}] {line}", file=stream)