0.7 Теперь появился логгер на старты
This commit is contained in:
@@ -13,7 +13,21 @@ class Logs:
|
||||
"""Класс для логирования с разными уровнями через loguru."""
|
||||
|
||||
@staticmethod
|
||||
def debug(text: str = "Логирование!", log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
def start(text: str = "Логирование!", system: str = "PRIMO",
|
||||
log_type: str = "AEP", user: str = "@Console", message: Message = None) -> None:
|
||||
"""
|
||||
Логирует сообщение на уровне DEBUG.
|
||||
|
||||
:param text: Сообщение для логирования.
|
||||
:param log_type: Тип лога (например, "Logs").
|
||||
:param user: Имя пользователя или источник вызова лога.
|
||||
:param message: Сообщение от пользователя, если необходимо извлечь имя.
|
||||
"""
|
||||
logger.bind(system=system, user=user, log_type=log_type).log("START", text)
|
||||
|
||||
@staticmethod
|
||||
def debug(text: str = "Логирование!", system : str = "DEBUG",
|
||||
log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
"""
|
||||
Логирует сообщение на уровне DEBUG.
|
||||
|
||||
@@ -24,10 +38,11 @@ class Logs:
|
||||
"""
|
||||
if message:
|
||||
user = username(message)
|
||||
logger.bind(log_type=log_type, user=user).debug(text)
|
||||
logger.bind(system=system, log_type=log_type, user=user).debug(text)
|
||||
|
||||
@staticmethod
|
||||
def info(text: str = "Логирование!", log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
def info(text: str = "Логирование!", system : str = "PRIMO",
|
||||
log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
"""
|
||||
Логирует сообщение на уровне INFO.
|
||||
|
||||
@@ -38,10 +53,11 @@ class Logs:
|
||||
"""
|
||||
if message:
|
||||
user = username(message)
|
||||
logger.bind(log_type=log_type, user=user).info(text)
|
||||
logger.bind(system=system, log_type=log_type, user=user).info(text)
|
||||
|
||||
@staticmethod
|
||||
def warning(text: str = "Логирование!", log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
def warning(text: str = "Логирование!", system : str = "WARNING",
|
||||
log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
"""
|
||||
Логирует сообщение на уровне WARNING.
|
||||
|
||||
@@ -52,10 +68,11 @@ class Logs:
|
||||
"""
|
||||
if message:
|
||||
user = username(message)
|
||||
logger.bind(log_type=log_type, user=user).warning(text)
|
||||
logger.bind(system=system, log_type=log_type, user=user).warning(text)
|
||||
|
||||
@staticmethod
|
||||
def error(text: str = "Логирование!", log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
def error(text: str = "Логирование!", system : str = "ERROR",
|
||||
log_type: str = "Logs", user: str = "@Console", message: Message = None) -> None:
|
||||
"""
|
||||
Логирует сообщение на уровне ERROR.
|
||||
|
||||
@@ -66,4 +83,4 @@ class Logs:
|
||||
"""
|
||||
if message:
|
||||
user = username(message)
|
||||
logger.bind(log_type=log_type, user=user).error(text)
|
||||
logger.bind(system=system, log_type=log_type, user=user).error(text)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import sys
|
||||
from loguru import logger
|
||||
from ProjectsFiles import BotLogs
|
||||
from ProjectsFiles import BotLogs, ProjectPath
|
||||
|
||||
|
||||
# Создание обычного логгера + логгер в файл
|
||||
@@ -16,8 +16,18 @@ async def setup_logger() -> None:
|
||||
"""
|
||||
logger.remove() # Удаляем все логгеры
|
||||
|
||||
if BotLogs.permission or BotLogs.permission_to_file:
|
||||
# Добавляем новый уровень START
|
||||
logger.level("START", no=25, color="white", icon="🔸")
|
||||
|
||||
# Настройка логирования в консоль для каждого уровня
|
||||
if BotLogs.permission:
|
||||
logger.add(sys.stderr,
|
||||
colorize=True,
|
||||
format=BotLogs.start_text,
|
||||
level="START",
|
||||
filter=lambda record: record["level"].name == "START"
|
||||
)
|
||||
logger.add(sys.stderr,
|
||||
colorize=True,
|
||||
format=BotLogs.debug_text,
|
||||
@@ -39,21 +49,40 @@ async def setup_logger() -> None:
|
||||
level="ERROR",
|
||||
filter=lambda record: record["level"].name == "ERROR")
|
||||
|
||||
# Добавление логгера для записи в файл (закомментированное, по необходимости активируется)
|
||||
# Добавление логгера для записи в файл
|
||||
if BotLogs.permission_to_file:
|
||||
# Uncomment and adjust if file logging is required
|
||||
# logger.add(ProjectPath.log_file,
|
||||
# rotation=BotLogs.max_size,
|
||||
# format=BotLogs.info_text,
|
||||
# backtrace=True,
|
||||
# diagnose=True,
|
||||
# level="INFO",
|
||||
# filter=lambda record: record["level"].name == "INFO")
|
||||
# logger.add(ProjectPath.log_error_file,
|
||||
# rotation=BotLogs.max_size,
|
||||
# format=BotLogs.error_text,
|
||||
# backtrace=True,
|
||||
# diagnose=True,
|
||||
# level="ERROR",
|
||||
# filter=lambda record: record["level"].name == "ERROR")
|
||||
return
|
||||
logger.add(ProjectPath.start_log_file,
|
||||
rotation=BotLogs.max_size,
|
||||
format=BotLogs.start_text,
|
||||
backtrace=True,
|
||||
diagnose=True,
|
||||
level="START",
|
||||
filter=lambda record: record["level"].name == "START")
|
||||
logger.add(ProjectPath.debug_log_file,
|
||||
rotation=BotLogs.max_size,
|
||||
format=BotLogs.debug_text,
|
||||
backtrace=True,
|
||||
diagnose=True,
|
||||
level="DEBUG",
|
||||
filter=lambda record: record["level"].name == "DEBUG")
|
||||
logger.add(ProjectPath.info_log_file,
|
||||
rotation=BotLogs.max_size,
|
||||
format=BotLogs.info_text,
|
||||
backtrace=True,
|
||||
diagnose=True,
|
||||
level="INFO",
|
||||
filter=lambda record: record["level"].name == "INFO")
|
||||
logger.add(ProjectPath.warning_log_file,
|
||||
rotation=BotLogs.max_size,
|
||||
format=BotLogs.warning_text,
|
||||
backtrace=True,
|
||||
diagnose=True,
|
||||
level="WARNING",
|
||||
filter=lambda record: record["level"].name == "WARNING")
|
||||
logger.add(ProjectPath.error_log_file,
|
||||
rotation=BotLogs.max_size,
|
||||
format=BotLogs.error_text,
|
||||
backtrace=True,
|
||||
diagnose=True,
|
||||
level="ERROR",
|
||||
filter=lambda record: record["level"].name == "ERROR")
|
||||
|
||||
5
BotLibrary/samples/__init__.py
Normal file
5
BotLibrary/samples/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# BotLibrary/samples/__init__.py
|
||||
# Инициализация пакета samples, для создания шаблонов
|
||||
|
||||
# Экспортирование модулей во внешние слои проекта
|
||||
from .user_cmd_class import *
|
||||
121
BotLibrary/samples/user_cmd_class.py
Normal file
121
BotLibrary/samples/user_cmd_class.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# BotCode/routers/commands/user_cmd_class.py
|
||||
# Класс-шаблон для создания новых команд
|
||||
|
||||
from aiogram import Router, types, F
|
||||
from aiogram.enums import ChatAction
|
||||
from aiogram.filters import Command
|
||||
|
||||
from ProjectsFiles import BotVar
|
||||
from BotLibrary.validators import username
|
||||
from BotLibrary.loggers import Logs
|
||||
|
||||
# Настройки экспорта в модули
|
||||
__all__ = ("CommandHandler", )
|
||||
|
||||
# Класс-шаблон для команд
|
||||
class CommandHandler:
|
||||
def __init__(self, name: str, keywords : list, chat_action : bool = False,
|
||||
description: str = "Описание команды", text_msg : str = "Сообщение",
|
||||
keyboard = None, prefix = BotVar.prefix, callbackdata = None,
|
||||
ignore_case : bool = True, activate_keywoards : bool = True,
|
||||
activate_commands : bool = True, activate_callback : bool = True,
|
||||
media : str = "message", path_to_media : str = None, parse_mode : str = BotVar.parse_mode,
|
||||
disable_notification : bool = False,
|
||||
):
|
||||
|
||||
self.router = Router(name=f"{name}_router")
|
||||
self.name = name
|
||||
self.log_type = name.capitalize()
|
||||
self.description = description
|
||||
|
||||
self.keywords = keywords
|
||||
self.text_msg = text_msg
|
||||
self.parse_mode = parse_mode
|
||||
self.keyboard = keyboard
|
||||
self.chat_action = chat_action
|
||||
self.disable_notification = disable_notification
|
||||
|
||||
self.media = media.lower()
|
||||
self.path_to_media = path_to_media
|
||||
if callbackdata == "keywords":
|
||||
self.callbackdata = keywords
|
||||
else:
|
||||
self.callbackdata = callbackdata
|
||||
|
||||
|
||||
# Привязываем хэндлер к роутеру
|
||||
if activate_commands:
|
||||
self.router.message(Command(*keywords, prefix=prefix, ignore_case=ignore_case))(self.handler)
|
||||
if activate_keywoards:
|
||||
self.router.message(F.text.lower().in_(keywords))(self.handler)
|
||||
if activate_callback:
|
||||
self.router.message(F.text.lower().in_(callbackdata))(self.handler)
|
||||
|
||||
|
||||
async def handler(self, message: types.Message):
|
||||
"""Основной хэндлер команды."""
|
||||
try:
|
||||
Logs.info(log_type=self.log_type, user=username(message), text=f"использовал(а) команду /{self.name}")
|
||||
if self.media == "message":
|
||||
await message.reply(
|
||||
text=self.text_msg,
|
||||
reply_markup=self.keyboard() if self.keyboard else None,
|
||||
parse_mode=self.parse_mode,
|
||||
disable_notification=self.disable_notification,
|
||||
)
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.TYPING,
|
||||
)
|
||||
else:
|
||||
if self.media == "photo":
|
||||
await message.reply_photo(
|
||||
photo="https://vos-mo.ru/upload/iblock/329/g0s939ge8o1n8xp7rcqnw9kkz9mcfrg2/risunok.jpg",
|
||||
caption=self.text_msg)
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_PHOTO,
|
||||
)
|
||||
|
||||
if self.media == "gif":
|
||||
await message.reply_animation(
|
||||
animation="https://vos-mo.ru/upload/iblock/329/g0s939ge8o1n8xp7rcqnw9kkz9mcfrg2/risunok.jpg")
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_VIDEO,
|
||||
)
|
||||
|
||||
if self.media == "video":
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_VIDEO,
|
||||
)
|
||||
|
||||
if self.media == "videonote":
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_VIDEO_NOTE,
|
||||
)
|
||||
|
||||
if self.media == "audio":
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_VOICE,
|
||||
)
|
||||
|
||||
if self.media == "file":
|
||||
if self.chat_action:
|
||||
await message.bot.send_chat_action(
|
||||
chat_id=message.chat.id,
|
||||
action=ChatAction.UPLOAD_DOCUMENT,
|
||||
)
|
||||
|
||||
# Проверка на ошибку
|
||||
except Exception as e:
|
||||
Logs.error(log_type=self.log_type, user=username(message), text=f"Ошибка команды: {e}")
|
||||
@@ -2,7 +2,6 @@
|
||||
# Создание и настройка бота в одном файле
|
||||
|
||||
from aiogram import Dispatcher, Bot, F
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder, ReplyKeyboardBuilder
|
||||
|
||||
@@ -34,12 +33,15 @@ dp["database"] = None
|
||||
|
||||
# Настройки для бота
|
||||
bot_properties = DefaultBotProperties(
|
||||
parse_mode=ParseMode.HTML, # Устанавливаем формат HTML для всех сообщений
|
||||
disable_notification=True, # Отключаем уведомления при отправке сообщений
|
||||
protect_content=True, # Защищаем содержимое сообщений от копирования
|
||||
allow_sending_without_reply=True, # Разрешаем отправлять сообщения без ответа на другое сообщение
|
||||
link_preview_is_disabled=True, # Отключаем предварительный просмотр ссылок
|
||||
show_caption_above_media=False, # Показываем подпись выше медиа
|
||||
parse_mode=BotVar.parse_mode, # Устанавливаем формат HTML для всех сообщений
|
||||
disable_notification=BotVar.disable_notification, # Отключаем уведомления при отправке сообщений
|
||||
protect_content=BotVar.protect_content, # Защищаем содержимое сообщений от копирования
|
||||
allow_sending_without_reply=BotVar.allow_sending_without_reply, # Разрешаем отправлять сообщения без ответа на другое сообщение
|
||||
link_preview_is_disabled=BotVar.link_preview_is_disabled, # Отключаем предварительный просмотр ссылок
|
||||
link_preview_prefer_small_media=BotVar.link_preview_prefer_small_media,
|
||||
link_preview_prefer_large_media=BotVar.link_preview_prefer_large_media,
|
||||
link_preview_show_above_text=BotVar.link_preview_show_above_text,
|
||||
show_caption_above_media=BotVar.show_caption_above_media, # Показываем подпись выше медиа
|
||||
)
|
||||
|
||||
# Создание экземпляра бота
|
||||
|
||||
@@ -6,9 +6,15 @@ from ProjectsFiles import ProjectPath, TypeDirectory
|
||||
from typing import List
|
||||
|
||||
# Настройка экспорта из модуля
|
||||
__all__ = ("create_directories", "setup_directories")
|
||||
__all__ = ("create_directories", "setup_directories", "create_directory")
|
||||
|
||||
|
||||
# Функция создания директории
|
||||
async def create_directory(directory : str) -> None:
|
||||
os.makedirs(directory)
|
||||
|
||||
|
||||
# Функция создания поддиректорий
|
||||
async def create_directories(base_directory: str, subdirectories: List[str]) -> None:
|
||||
"""
|
||||
Создает указанные поддиректории в указанной базовой директории, если они еще не существуют.
|
||||
@@ -26,6 +32,7 @@ async def create_directories(base_directory: str, subdirectories: List[str]) ->
|
||||
print(f"Создана директория: {directory_path}")
|
||||
|
||||
|
||||
# Функция установки начальных директорий
|
||||
async def setup_directories() -> None:
|
||||
"""
|
||||
Настройка начальных пустых директорий для проекта.
|
||||
|
||||
Reference in New Issue
Block a user