GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 13/15] telegram: logger: Refactor all logging method
Date: Wed, 18 Jan 2023 05:12:14 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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 <[email protected]>
 #
 
+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


  parent reply	other threads:[~2023-01-17 22:13 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 22:12 [PATCH v1 00/15] Everything about logger changes and some fixes Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 01/15] telegram: Simplify code to get DB_PORT from env Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 02/15] discord: " Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 03/15] telegram: logger: Add a telegram.logger.conf Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 04/15] discord: logger: Add a discord.logger.conf Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 05/15] telegram: logger: Initialize the configuration for the Telegram logger Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 06/15] discord: logger: Initialize the configuration for the Discord logger Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 07/15] telegram: fix: Fix the type annoations for the decorator Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 08/15] discord: cleanup: Remove some unnecessary comments Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 09/15] discord: fix: Fix the type annotations for the decorator Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 10/15] discord: typing: Add return type annotations Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 11/15] telegram: Implement DaemonException() and report_err() in scrape.py Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 12/15] utils: fix: Fix charset issue for get_decoded_payload() Muhammad Rizki
2023-01-17 22:12 ` Muhammad Rizki [this message]
2023-01-17 22:12 ` [PATCH v1 14/15] discord: Implement DaemonException and report_err in get_lore_mail.py Muhammad Rizki
2023-01-17 22:12 ` [PATCH v1 15/15] discord: logger: Refactor all logging method Muhammad Rizki
2023-01-17 23:10 ` [PATCH v1 00/15] Everything about logger changes and some fixes Ammar Faizi
2023-01-17 23:29   ` Muhammad Rizki
2023-01-17 23:31     ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox