Developing Bot
This commit is contained in:
@@ -1,6 +1,29 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||
import configparser
|
||||
import json
|
||||
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)
|
||||
Reference in New Issue
Block a user