Files
pawhub-telegram-bot/log.py
T
2025-12-24 02:06:17 +01:00

29 lines
1.0 KiB
Python

import os
from config import config
from datetime import datetime
LOG_ID_INFO = 'I'
LOG_ID_WARNING = 'W'
LOG_ID_ERROR = 'E'
def info(chat_id: str, message: str) -> None:
log_line = format_message(LOG_ID_INFO, chat_id, message)
write_log(log_line)
def warning(chat_id: str, message: str) -> None:
log_line = format_message(LOG_ID_WARNING, chat_id, message)
write_log(log_line)
def error(chat_id: str, message: str, stack_trace: str) -> None:
log_line = format_message(LOG_ID_ERROR, chat_id, message)
log_line += f'\nStack Trace:\n{stack_trace}'
write_log(log_line)
def format_message (log_id: str, chat_id: str, message: str) -> str:
return f'{datetime.now().strftime("%Y-%m-%d @ %H:%M")} | {config.BOT_NAME} | {log_id} | {message}'
def write_log(log_line: str):
os.makedirs(config.LOG_FOLDER_PATH, exist_ok=True)
with open(os.path.join(config.LOG_FOLDER_PATH, config.LOG_FILE_NAME), 'a', encoding='utf-8') as log_file:
log_file.write(log_line + '\n')
print(log_line)