From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [175.158.50.43]) by gnuweeb.org (Postfix) with ESMTPSA id 8A52281A19; Wed, 18 Jan 2023 17:54:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674064483; bh=Nxezgghhd8w+N2n6I5u94PMHz7pEpwz6Dy+SQP/TvQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lEAR8kOtOytuurjtgLmYHXq9+j051yV+WI3FHcWh2JHqU+n9tvssP2fFyaUdmLQHJ nGRX8Ry4RieXKjDbb0lG1eqqEt+ba6psqfrh08mk3FVZXF/dbH/PDFwndrHfnW18/4 F0KDi68E0dz0iFx00iNTBQbqnLYDJmAc6V1gVdKJZQbnHpVWjJPs39SQGog7SxMEZf fcGzaADg6P8CVEO0f/zqb77T86jJ9e8w2BJhowBLM+x2zeYy+MarGBLWMg1BUDaxeV CzBdk47kbNeBWQCtAbesgAPYsJ1Sb6td7PiEujfih+fEvKvs3fh+UP2lhub/OgYK5k Fl6DbJ/SCYUqw== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v2 13/17] telegram: logger: Refactor all logging method Date: Thu, 19 Jan 2023 00:53:52 +0700 Message-Id: <20230118175356.1853-14-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20230118175356.1853-1-kiizuha@gnuweeb.org> References: <20230118175356.1853-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We use a simple logging method by using file config and replace all custom logger method to built-in logging method. This method is more cleaner and less code wasteful. The reason why we should name the logger like: `logging.getLogger("telegram")` Because, we need to identify which bot platform that currently running. Signed-off-by: Muhammad Rizki --- daemon/telegram/mailer/listener.py | 15 +++++++++------ daemon/telegram/packages/client.py | 21 +++++++++++++-------- daemon/telegram/packages/decorator.py | 14 +++++++------- daemon/tg.py | 6 ------ 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 2e8eeda..b34c74d 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -15,6 +15,10 @@ from atom import utils from enums import Platform import asyncio import re +import logging + + +log = logging.getLogger("telegram") class BotMutexes(): @@ -30,7 +34,6 @@ class Bot(): self.scraper = scraper self.mutexes = mutexes self.db = client.db - self.logger = client.logger self.isRunnerFixed = False @@ -57,8 +60,8 @@ class Bot(): # TODO(ammarfaizi2): # Ideally, we also want to log and report this situation. # - self.logger.error(f"Database error: {str(e)}") - self.logger.info("Reconnecting to the database...") + log.error(f"Database error: {str(e)}") + log.info("Reconnecting to the database...") # # Don't do this too often to avoid reconnect burst. @@ -71,7 +74,7 @@ class Bot(): async def __run(self): - self.logger.info("Running...") + log.info("Running...") url = None try: for url in self.db.get_atom_urls(): @@ -128,14 +131,14 @@ class Bot(): if not email_msg_id: md = "email_msg_id not detected, skipping malformed email" - self.logger.debug(md) + log.debug(md) return False email_id = self.__mail_id_from_db(email_msg_id, tg_chat_id) if not email_id: md = f"Skipping {email_id} because has already been sent to Telegram" - self.logger.debug(md) + log.debug(md) return False text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py index a58aaf8..7e898c5 100644 --- a/daemon/telegram/packages/client.py +++ b/daemon/telegram/packages/client.py @@ -3,41 +3,46 @@ # Copyright (C) 2022 Muhammad Rizki # +import logging from pyrogram import Client from pyrogram.enums import ParseMode from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton from typing import Union from email.message import Message + from atom import utils from enums import Platform -from logger import BotLogger from telegram import config from telegram.database import DB from .decorator import handle_flood from exceptions import DaemonException +log = logging.getLogger("telegram") + + class DaemonTelegram(Client): def __init__(self, name: str, api_id: int, - api_hash: str, conn, logger: BotLogger, - **kwargs + api_hash: str, conn, **kwargs ): super().__init__(name, api_id, api_hash, **kwargs) self.db = DB(conn) - self.logger = logger async def report_err(self, e: DaemonException): capt = f"Atom URL: {e.atom_url}\n" capt += f"Thread URL: {e.thread_url}" - self.logger.warning(e.original_exception) + log.warning(e.original_exception) await self.send_log_file(capt) @handle_flood async def send_log_file(self, caption: str): - filename = self.logger.handlers[0].baseFilename + for handler in log.root.handlers: + if isinstance(handler, logging.FileHandler): + filename = handler.baseFilename + await self.send_document( config.LOG_CHANNEL_ID, filename, @@ -54,7 +59,7 @@ class DaemonTelegram(Client): url: str = None, parse_mode: ParseMode = ParseMode.HTML ) -> Message: - self.logger.debug("[send_text_email]") + log.debug("[send_text_email]") return await self.send_message( chat_id=chat_id, text=text, @@ -79,7 +84,7 @@ class DaemonTelegram(Client): url: str = None, parse_mode: ParseMode = ParseMode.HTML ) -> Message: - self.logger.debug("[send_patch_email]") + log.debug("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( mail, text, url, Platform.TELEGRAM ) diff --git a/daemon/telegram/packages/decorator.py b/daemon/telegram/packages/decorator.py index 6bfd8bc..64162f6 100644 --- a/daemon/telegram/packages/decorator.py +++ b/daemon/telegram/packages/decorator.py @@ -9,9 +9,12 @@ from typing_extensions import ParamSpec, ParamSpecArgs, ParamSpecKwargs from functools import wraps import re import asyncio +import logging + __all__ = ["handle_flood"] +log = logging.getLogger("telegram") T = TypeVar("T") P = ParamSpec("P") @@ -23,19 +26,16 @@ def handle_flood(func: Callable[P, Coroutine[Any,Any,T]]) -> Callable[P, Corouti try: return await func(*args, **kwargs) except FloodWait as e: - # Calling logger attr from the DaemonTelegram() class - logger = args[0].logger - - _flood_exceptions(e, logger) - logger.info("Woken up from flood wait...") + _flood_exceptions(e) + log.info("Woken up from flood wait...") return callback -async def _flood_exceptions(e, logger): +async def _flood_exceptions(e): x = re.search(r"A wait of (\d+) seconds is required", str(e)) if not x: raise e n = int(x.group(1)) - logger.info(f"Sleeping for {n} seconds due to Telegram limit") + log.info(f"Sleeping for {n} seconds due to Telegram limit") await asyncio.sleep(n) diff --git a/daemon/tg.py b/daemon/tg.py index 26f8374..5233faa 100644 --- a/daemon/tg.py +++ b/daemon/tg.py @@ -7,12 +7,10 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from dotenv import load_dotenv from mysql import connector -from pyrogram import idle from atom import Scraper from telegram.packages import DaemonTelegram from telegram.mailer import BotMutexes from telegram.mailer import Bot -from logger import BotLogger import os import logging import logging.config @@ -21,9 +19,6 @@ import logging.config def main(): load_dotenv("telegram.env") - logger = BotLogger() - logger.init() - logging.config.fileConfig("telegram/telegram.logger.conf") logging.getLogger("apscheduler").setLevel(logging.WARNING) logging.getLogger("pyrogram").setLevel(logging.WARNING) @@ -33,7 +28,6 @@ def main(): api_id=int(os.getenv("API_ID")), api_hash=os.getenv("API_HASH"), bot_token=os.getenv("BOT_TOKEN"), - logger=logger, conn=connector.connect( host=os.getenv("DB_HOST"), user=os.getenv("DB_USER"), -- Muhammad Rizki