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 DB53E7F937; Wed, 18 Jan 2023 17:54:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1674064487; bh=p0BvEDxsAuT1txKkMF/qD4G5y9+oyBbty/+onm4zRcQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=abVG58TQPiug1G03JDD6iamcQlh3rQr/gIkY+g8x8j1nwCds9gOFeWm+7JAyHNYF/ TsKYRiUC5pQ0L24i+O6lawK8tFe+mbhrjHVcfRoG9vQMlMwaAv7telClujHZWp/vxj xHRDB6AD5lnl+McEjmZ/4VFkcYJgJjcfTJCR77qw42hB9lkT3uOEgODXTRPUUGPcu3 0nygWeX0ItXvL20SwBe7u2YNUOMFdiIQngtlOaisigkMFrx6nmdzBIO/Hbg33LIsFV Es5C/LMndZtLpHfsw2ZYI5XEjzvoCm0QCkmi7UPNBAXCng7/tVCT7nFUelF1FYNSQ3 M9R4P3PH1WVLQ== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v2 15/17] discord: logger: Refactor all logging method Date: Thu, 19 Jan 2023 00:53:54 +0700 Message-Id: <20230118175356.1853-16-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: Same as the telegram refactors. 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("dscord")` Because, if we use "discord" as the logger name, it will conflict the discord.py's logger, that's why we need to disable the discord.py's logger not to interrupt our logger. Signed-off-by: Muhammad Rizki --- daemon/dc.py | 8 +------- daemon/dscord/gnuweeb/client.py | 19 ++++++++++++------- daemon/dscord/gnuweeb/filters.py | 12 +++++------- .../dscord/gnuweeb/plugins/events/on_ready.py | 5 ++++- daemon/dscord/mailer/listener.py | 17 ++++++++++------- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/daemon/dc.py b/daemon/dc.py index f4eeefb..dade9f1 100644 --- a/daemon/dc.py +++ b/daemon/dc.py @@ -13,8 +13,6 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from dscord.gnuweeb import GWClient from dscord.mailer import Listener from dscord.mailer import Mutexes -from enums.platform import Platform -from logger import BotLogger from atom import Scraper @@ -28,9 +26,6 @@ def main(): } ) - logger = BotLogger(Platform.DISCORD) - logger.init() - logging.config.fileConfig("dscord/discord.logger.conf") logging.getLogger("apscheduler").setLevel(logging.WARNING) logging.getLogger("discord").setLevel(logging.WARNING) @@ -42,8 +37,7 @@ def main(): port=int(os.environ.get("DB_PORT", 3306)), password=os.getenv("DB_PASS"), database=os.getenv("DB_NAME") - ), - logger=logger + ) ) mailer = Listener( diff --git a/daemon/dscord/gnuweeb/client.py b/daemon/dscord/gnuweeb/client.py index 7d6433b..a04fd26 100644 --- a/daemon/dscord/gnuweeb/client.py +++ b/daemon/dscord/gnuweeb/client.py @@ -3,6 +3,7 @@ # Copyright (C) 2022 Muhammad Rizki # +import logging import discord from discord import Interaction, Message from discord.ext import commands @@ -14,18 +15,19 @@ from . import models from atom import utils from enums import Platform from exceptions import DaemonException -from logger.log import BotLogger from dscord.config import ACTIVITY_NAME, LOG_CHANNEL_ID from dscord.database import DB +log = logging.getLogger("dscord") + + class GWClient(commands.Bot): - def __init__(self, db_conn, logger: BotLogger) -> None: + def __init__(self, db_conn) -> None: self.db = DB(db_conn) intents = Intents.default() intents.message_content = True self.mailer = None - self.logger = logger super().__init__( command_prefix=["$", "."], description="Just a bot for receiving lore emails.", @@ -42,7 +44,7 @@ class GWClient(commands.Bot): async def report_err(self, e: DaemonException): - self.logger.warning(e.original_exception) + log.warning(e.original_exception) capt = f"Atom URL: {e.atom_url}\n" capt += f"Thread URL: {e.thread_url}" await self.send_log_file(capt) @@ -50,7 +52,10 @@ class GWClient(commands.Bot): @filters.wait_on_limit 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 + channel = self.get_channel(LOG_CHANNEL_ID) await channel.send( content=caption, @@ -61,7 +66,7 @@ class GWClient(commands.Bot): @filters.wait_on_limit async def send_text_email(self, guild_id: int, chat_id: int, text: str, reply_to: Union[int, None] = None, url: str = None) -> Message: - self.logger.debug("[send_text_email]") + log.debug("[send_text_email]") channel = self.get_channel(chat_id) return await channel.send( @@ -78,7 +83,7 @@ class GWClient(commands.Bot): @filters.wait_on_limit async def send_patch_email(self, mail, guild_id: int, chat_id: int, text: str, reply_to: Union[int, None] = None, url: str = None) -> Message: - self.logger.debug("[send_patch_email]") + log.debug("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( mail, text, url, Platform.DISCORD ) diff --git a/daemon/dscord/gnuweeb/filters.py b/daemon/dscord/gnuweeb/filters.py index 28b492c..ccd13e7 100644 --- a/daemon/dscord/gnuweeb/filters.py +++ b/daemon/dscord/gnuweeb/filters.py @@ -5,6 +5,7 @@ import asyncio +import logging from typing import Any, Callable, TypeVar, Coroutine from typing_extensions import ParamSpec, ParamSpecArgs, ParamSpecKwargs from functools import wraps @@ -13,9 +14,9 @@ import discord from discord import Interaction from dscord import config -from logger import BotLogger +log = logging.getLogger("dscord") T = TypeVar("T") P = ParamSpec("P") @@ -45,15 +46,12 @@ def wait_on_limit(func: Callable[P, Coroutine[Any,Any,T]]) -> Callable[P, Corout try: return await func(*args, **kwargs) except discord.errors.RateLimited as e: - # Calling logger attr from the GWClient() class - logger = args[0].logger - _flood_exceptions(e) - logger.info("Woken up from flood wait...") + log.info("Woken up from flood wait...") return callback -async def _flood_exceptions(e: "discord.errors.RateLimited", logger: BotLogger): +async def _flood_exceptions(e: "discord.errors.RateLimited"): wait = e.retry_after - logger.info(f"Sleeping for {wait} seconds due to Discord limit") + log.info(f"Sleeping for {wait} seconds due to Discord limit") await asyncio.sleep(wait) diff --git a/daemon/dscord/gnuweeb/plugins/events/on_ready.py b/daemon/dscord/gnuweeb/plugins/events/on_ready.py index e7f63cd..4248aad 100644 --- a/daemon/dscord/gnuweeb/plugins/events/on_ready.py +++ b/daemon/dscord/gnuweeb/plugins/events/on_ready.py @@ -3,9 +3,12 @@ # Copyright (C) 2022 Muhammad Rizki # +import logging from discord.ext import commands +log = logging.getLogger("dscord") + class OnReady(commands.Cog): def __init__(self, bot: "commands.Bot") -> None: self.bot = bot @@ -23,4 +26,4 @@ class OnReady(commands.Cog): t += f"Send `{prefix}sync` message to the Discord channel " t += "where the bot is running.\n" - self.bot.logger.info(t) + log.info(t) diff --git a/daemon/dscord/mailer/listener.py b/daemon/dscord/mailer/listener.py index fc066b7..25e5715 100644 --- a/daemon/dscord/mailer/listener.py +++ b/daemon/dscord/mailer/listener.py @@ -6,6 +6,7 @@ import asyncio import re +import logging from mysql.connector.errors import OperationalError, DatabaseError from apscheduler.schedulers.asyncio import AsyncIOScheduler from discord import File @@ -18,6 +19,9 @@ from enums import Platform from exceptions import DaemonException +log = logging.getLogger("dscord") + + class Mutexes: def __init__(self): self.lock = asyncio.Lock() @@ -36,7 +40,6 @@ class Listener: self.scraper = scraper self.mutexes = mutexes self.db = client.db - self.logger = client.logger self.isRunnerFixed = False self.runner = None @@ -46,7 +49,7 @@ class Listener: # Execute __run() once to avoid high latency at # initilization. # - self.logger.info("Initialize listener...\n") + log.info("Initialize listener...\n") self.sched.start() self.runner = self.sched.add_job(func=self.__run) @@ -56,8 +59,8 @@ class Listener: # 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. @@ -70,7 +73,7 @@ class Listener: async def __run(self): - self.logger.info("Running...") + log.info("Running...") url = None try: @@ -128,7 +131,7 @@ class Listener: email_msg_id = utils.get_email_msg_id(mail) 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.__get_email_id_sent( @@ -137,7 +140,7 @@ class Listener: ) if not email_id: md = f"Skipping {email_id} because has already been sent to Discord" - self.logger.debug(md) + log.debug(md) return False text, files, is_patch = utils.create_template(mail, Platform.DISCORD) -- Muhammad Rizki