Created
June 19, 2024 12:30
-
-
Save IvanIsak2000/92099dd42c914313f0227ff18b0e801e to your computer and use it in GitHub Desktop.
Aiogram logger alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from logtail import LogtailHandler | |
import logging | |
import requests | |
from utils.config import LOG_TOKEN | |
from utils.config import BOT_KEY | |
from utils.config import MODERATOR_ID | |
class AiogramAlertLogger(): | |
""" | |
Class for basic logging and send alert to Telegram's main | |
moderator using Logtail monitoring. | |
""" | |
def __init__(self) -> None: | |
handler = LogtailHandler(source_token=LOG_TOKEN) | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.INFO) | |
logger.handlers = [] | |
logger.addHandler(handler) | |
self.logger = logger | |
async def send_alert_to_main_moderator( | |
self, | |
text: str, | |
extra: str = None | |
) -> None: | |
url = f'https://api.telegram.org/bot{BOT_KEY}/sendMessage' | |
params = { | |
'chat_id': MODERATOR_ID, | |
'text': f'ALERT:\n{text}'} | |
requests.post(url, params=params) | |
async def info( | |
self, | |
message: str, | |
send_alert: bool = False, | |
extra: dict = None | |
) -> None: | |
if send_alert: | |
self.logger.info(msg=message, extra=extra) | |
await self.send_alert_to_main_moderator( | |
text=message) | |
else: | |
self.logger.info(msg=message) | |
async def error( | |
self, | |
message: str | |
) -> None: | |
self.logger.error(msg=message) | |
await self.send_alert_to_main_moderator( | |
text=message | |
) | |
async def critical( | |
self, | |
message: str, | |
extra: dict = None | |
) -> None: | |
self.logger.critical(msg=message, extra=extra) | |
await self.send_alert_to_main_moderator( | |
text=message, | |
extra=extra | |
) | |
logger = AlertLogger() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment