Skip to content

Commit

Permalink
Apply loguru instead logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mezgoodle committed Jul 2, 2022
1 parent d6275a4 commit 4b06f19
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 39 deletions.
12 changes: 5 additions & 7 deletions bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 0 additions & 4 deletions tgbot/handlers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}!')
4 changes: 0 additions & 4 deletions tgbot/handlers/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 5 additions & 6 deletions tgbot/handlers/errors.py
Original file line number Diff line number Diff line change
@@ -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}')
5 changes: 0 additions & 5 deletions tgbot/handlers/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 0 additions & 5 deletions tgbot/handlers/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

from aiogram.dispatcher import FSMContext
from aiogram.types import Message

Expand All @@ -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}')
5 changes: 0 additions & 5 deletions tgbot/handlers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 1 addition & 3 deletions tgbot/services/admins_notify.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 4b06f19

Please sign in to comment.