Files
telegram-helper-bot/logs/custom_logger.py
2024-07-11 22:59:58 +03:00

46 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import datetime
import os
import loguru
class Logger:
def __init__(self, name):
self.logger = loguru.logger.bind(name=name)
# Получение сегодняшней даты для имени файла
today = datetime.date.today().strftime('%Y-%m-%d')
# Создание папки для логов
current_dir = os.path.dirname(os.path.abspath(__file__))
if not os.path.exists(current_dir):
# Если не существует, создаем ее
os.makedirs(current_dir)
filename = f'{current_dir}/helper_bot_{today}.log'
# Настройка формата логов
self.logger.add(
filename,
rotation="00:00",
retention="5 days",
compression="zip",
format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {name} | {line} | {message}",
)
def get_logger(self):
return self.logger
def info(self, message):
self.logger.info(message)
def debug(self, message):
self.logger.debug(message)
def warning(self, message):
self.logger.warning(message)
def error(self, message):
self.logger.error(message)
def critical(self, message):
self.logger.critical(message)