This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/modules/logging.py

87 lines
2.6 KiB
Python
Raw Normal View History

2022-10-17 00:30:07 +03:00
from ujson import loads
from os import stat, makedirs, path, getcwd
from gzip import open as gzipopen
from shutil import copyfileobj
from datetime import datetime
2023-03-09 17:25:06 +02:00
with open(getcwd() + path.sep + "config.json", "r", encoding="utf8") as file:
2022-10-17 00:30:07 +03:00
json_contents = loads(file.read())
log_size = json_contents["logging"]["size"]
log_folder = json_contents["logging"]["location"]
file.close()
2023-03-09 17:25:06 +02:00
2022-10-17 00:30:07 +03:00
# Check latest log size
def checkSize(debug=False):
2022-12-14 15:57:39 +02:00
"""Check size of latest.log file and rotate it if needed
### Args:
* debug (`bool`, *optional*): Whether this is a debug log. Defaults to `False`.
2023-03-09 17:25:06 +02:00
"""
2022-10-17 00:30:07 +03:00
global log_folder
if debug:
log_file = "debug.log"
else:
log_file = "latest.log"
try:
makedirs(log_folder, exist_ok=True)
log = stat(path.join(log_folder, log_file))
if (log.st_size / 1024) > log_size:
2023-03-09 17:25:06 +02:00
with open(path.join(log_folder, log_file), "rb") as f_in:
with gzipopen(
path.join(
log_folder,
f'{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz',
),
"wb",
) as f_out:
2022-10-17 00:30:07 +03:00
copyfileobj(f_in, f_out)
2023-03-09 17:25:06 +02:00
print(
f'Copied {path.join(log_folder, datetime.now().strftime("%d.%m.%Y_%H:%M:%S"))}.log.gz'
)
open(path.join(log_folder, log_file), "w").close()
2022-10-17 00:30:07 +03:00
except FileNotFoundError:
2023-03-09 17:25:06 +02:00
print(f"Log file {path.join(log_folder, log_file)} does not exist")
2022-10-17 00:30:07 +03:00
pass
2023-03-09 17:25:06 +02:00
2022-10-17 00:30:07 +03:00
# Append string to log
2022-12-14 15:57:39 +02:00
def logAppend(message: str, debug=False):
"""Write message to log file
### Args:
* message (`str`): Message to write
* debug (`bool`, *optional*): Whether this is a debug log. Defaults to `False`.
2023-03-09 17:25:06 +02:00
"""
2022-10-17 00:30:07 +03:00
global log_folder
message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] {message}'
checkSize(debug=debug)
if debug:
log_file = "debug.log"
else:
log_file = "latest.log"
2023-03-09 17:25:06 +02:00
log = open(path.join(log_folder, log_file), "a")
log.write(f"{message_formatted}\n")
2022-10-17 00:30:07 +03:00
log.close()
2023-03-09 17:25:06 +02:00
2022-10-17 00:30:07 +03:00
# Print to stdout and then to log
2022-12-14 15:57:39 +02:00
def logWrite(message: str, debug=False):
"""Write message to stdout and log file
### Args:
* message (`str`): Message to print and write
* debug (`bool`, *optional*): Whether this is a debug log. Defaults to `False`.
2023-03-09 17:25:06 +02:00
"""
2022-10-17 00:30:07 +03:00
# save to log file and rotation is to be done
2023-03-09 17:25:06 +02:00
logAppend(f"{message}", debug=debug)
print(f"{message}", flush=True)