From 4b06f19a0608b2c5638f46d957619e6910323618 Mon Sep 17 00:00:00 2001 From: Maxim Zavalniuk Date: Sat, 2 Jul 2022 21:08:57 +0300 Subject: [PATCH] Apply loguru instead logging --- bot.py | 12 +++++------- tgbot/handlers/admin.py | 4 ---- tgbot/handlers/echo.py | 4 ---- tgbot/handlers/errors.py | 11 +++++------ tgbot/handlers/help.py | 5 ----- tgbot/handlers/state.py | 5 ----- tgbot/handlers/user.py | 5 ----- tgbot/services/admins_notify.py | 4 +--- 8 files changed, 11 insertions(+), 39 deletions(-) diff --git a/bot.py b/bot.py index 95a7f0f..801c5b8 100644 --- a/bot.py +++ b/bot.py @@ -1,9 +1,9 @@ -import functools import logging import os from aiogram import Dispatcher from aiogram.utils.executor import start_polling, start_webhook +from loguru import logger from tgbot.config import load_config from tgbot.filters.admin import IsAdminFilter @@ -12,8 +12,6 @@ from tgbot.services.admins_notify import on_startup_notify from loader import dp -logger = logging.getLogger(__name__) - def register_all_middlewares(dispatcher: Dispatcher) -> None: logger.info('Registering middlewares') @@ -62,12 +60,12 @@ async def on_shutdown(dispatcher: Dispatcher) -> None: if __name__ == '__main__': - logging.basicConfig( - level=logging.INFO, - format=u'%(filename)s:%(lineno)d #%(levelname)-8s [%(asctime)s] - %(name)s - %(message)s', - ) + logger.add('tgbot.log', format='{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}', rotation='10 KB', + compression='zip') config = load_config() + logger.info('Initializing bot') + # Webhook settings HEROKU_APP_NAME = os.getenv('HEROKU_APP_NAME') WEBHOOK_HOST = f'https://{HEROKU_APP_NAME}.herokuapp.com' diff --git a/tgbot/handlers/admin.py b/tgbot/handlers/admin.py index 35a7cd7..0da8a7a 100644 --- a/tgbot/handlers/admin.py +++ b/tgbot/handlers/admin.py @@ -3,11 +3,7 @@ from loader import dp -import logging - @dp.message_handler(CommandStart(), state='*', is_admin=True) async def admin_command(message: Message) -> Message: - logger = logging.getLogger(__name__) - logger.info('Handler executed') await message.reply(f'Hello, {message.from_user.username}!') diff --git a/tgbot/handlers/echo.py b/tgbot/handlers/echo.py index ec99396..ee0ff4a 100644 --- a/tgbot/handlers/echo.py +++ b/tgbot/handlers/echo.py @@ -2,11 +2,7 @@ from loader import dp -import logging - @dp.message_handler() async def echo(message: Message) -> Message: - logger = logging.getLogger(__name__) - logger.info('Handler executed') await message.answer(message.text) diff --git a/tgbot/handlers/errors.py b/tgbot/handlers/errors.py index d0a0360..263fa90 100644 --- a/tgbot/handlers/errors.py +++ b/tgbot/handlers/errors.py @@ -1,31 +1,30 @@ -import logging from typing import Optional from aiogram.utils.exceptions import (TelegramAPIError, MessageNotModified, CantParseEntities) +from loguru import logger from loader import dp -logger = logging.getLogger(__name__) @dp.errors_handler() async def errors_handler(update, exception) -> Optional[bool]: if isinstance(exception, MessageNotModified): - logger.exception('Message is not modified') + logger.error('Message is not modified') # do something here? return True if isinstance(exception, CantParseEntities): # or here - logger.exception(f'CantParseEntities: {exception} \nUpdate: {update}') + logger.error(f'CantParseEntities: {exception} \nUpdate: {update}') return True # MUST BE THE LAST CONDITION if isinstance(exception, TelegramAPIError): - logger.exception(f'TelegramAPIError: {exception} \nUpdate: {update}') + logger.error(f'TelegramAPIError: {exception} \nUpdate: {update}') return True # At least you have tried. - logger.exception(f'Update: {update} \n{exception}') + logger.error(f'Update: {update} \n{exception}') diff --git a/tgbot/handlers/help.py b/tgbot/handlers/help.py index ce55a07..2708164 100644 --- a/tgbot/handlers/help.py +++ b/tgbot/handlers/help.py @@ -4,13 +4,8 @@ from loader import dp from tgbot.keyboards.inline.help_keyboard import create_markup -import logging - @dp.message_handler(CommandHelp(), state='*') async def help_command(message: Message) -> Message: - logger = logging.getLogger(__name__) - logger.info('Handler executed') - markup = create_markup() await message.reply(f'Hello, {message.from_user.username}!', reply_markup=markup) diff --git a/tgbot/handlers/state.py b/tgbot/handlers/state.py index e178a0a..0760c3f 100644 --- a/tgbot/handlers/state.py +++ b/tgbot/handlers/state.py @@ -1,5 +1,3 @@ -import logging - from aiogram.dispatcher import FSMContext from aiogram.types import Message @@ -9,8 +7,5 @@ @dp.message_handler(state=Example.example) async def state_handler(message: Message, state: FSMContext) -> Message: - logger = logging.getLogger(__name__) - logger.info('Handler executed') - await state.finish() await message.reply(f'Your state: {message.text}') diff --git a/tgbot/handlers/user.py b/tgbot/handlers/user.py index 6e08b8f..5795acc 100644 --- a/tgbot/handlers/user.py +++ b/tgbot/handlers/user.py @@ -6,15 +6,10 @@ from tgbot.states.states import Example from tgbot.middlewares.throttling import rate_limit -import logging - @dp.message_handler(CommandStart(), state="*") @rate_limit(5, 'start') async def user_command(message: Message) -> Message: - logger = logging.getLogger(__name__) - logger.info('Handler executed') - await Example.first() markup = create_markup() await message.reply("Hello, user!", reply_markup=markup) diff --git a/tgbot/services/admins_notify.py b/tgbot/services/admins_notify.py index 8d05208..fc27260 100644 --- a/tgbot/services/admins_notify.py +++ b/tgbot/services/admins_notify.py @@ -1,13 +1,11 @@ from aiogram import Dispatcher from aiogram.utils.exceptions import ChatNotFound +from loguru import logger from tgbot.config import load_config -import logging - async def on_startup_notify(dp: Dispatcher): - logger = logging.getLogger(__name__) logger.info('Start admins notification') config = load_config()