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 autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.209]) by gnuweeb.org (Postfix) with ESMTPSA id 6126F8191C; Mon, 19 Dec 2022 23:58:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671494282; bh=/JVpiRkO6heX2kLHoInb8NHNZ7mP44LFSarydnDh3C4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ke2NifADR8xtiWR7ZhAfRaqtfiYB/bt4e61FyXxikxHK3lsBKtTf0kQpaPz8gDTMp tExeTWo38BawbqYi6bjz4hjI5kBftMXnisxopmVAtNYpk3lcMQOxo7FB2Ccwtr/FPV JXz1UKnBtjG8lFzcFlPUKXl+oClTN+D5wJS22YYetb2aODI3OL8pIy5z0UiRX/v4A5 iLvbiSa1ttRWUREQdexriEQcvDxX3fhDvC+zUo4XdEc3XriopCLGmReQa2rubjkalc 0sLb/F8fn8coJoFU4YNuIRuVEjtVUAoayNiSJIdPd/SQ3/q9s89Y6PE7DIEw17c25v DUNqdwny0rPBA== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v3 08/17] logger: Initial work for the bot logger for future use Date: Tue, 20 Dec 2022 06:57:12 +0700 Message-Id: <20221219235721.126-9-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221219235721.126-1-kiizuha@gnuweeb.org> References: <20221219235721.126-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add a BotLogger() class that inherit to Logger() class from the python logging library. Signed-off-by: Muhammad Rizki --- daemon/logger/__init__.py | 1 + daemon/logger/log.py | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 daemon/logger/__init__.py create mode 100644 daemon/logger/log.py diff --git a/daemon/logger/__init__.py b/daemon/logger/__init__.py new file mode 100644 index 0000000..bb3145a --- /dev/null +++ b/daemon/logger/__init__.py @@ -0,0 +1 @@ +from .log import BotLogger diff --git a/daemon/logger/log.py b/daemon/logger/log.py new file mode 100644 index 0000000..8382404 --- /dev/null +++ b/daemon/logger/log.py @@ -0,0 +1,52 @@ +import logging +import os +from enums import Platform + + +class BotLogger(logging.Logger): + + def __init__( + self, + platform: Platform = Platform.TELEGRAM, + level: int = logging.WARNING + ) -> None: + + self.platform = str(platform.value) + self.level = level + + plat_rep = self.platform.replace("discord", "dscord") + self.file_name = f"{plat_rep}/" \ + f"{os.getenv('STORAGE_DIR', 'storage')}/" \ + f"{self.platform}.log" + + self.file_fmt = "{" \ + "'time': '%(asctime)s'," \ + "'func': '%(name)s: %(funcName)s'," \ + "'path': '%(pathname)s'," \ + "'level': '%(levelname)s'," \ + "'msg': '%(message)s'"\ + "}" + + self.stream_fmt = "%(asctime)s | " \ + "%(name)s: %(funcName)s | " \ + "%(levelname)s | " \ + "%(message)s" + + + def init(self): + super().__init__(self.platform, self.level) + + file_handler = logging.FileHandler(self.file_name) + stream_handler = logging.StreamHandler() + + file_fmtr = logging.Formatter(self.file_fmt) + stream_fmtr = logging.Formatter(self.stream_fmt) + + file_handler.setFormatter(file_fmtr) + stream_handler.setFormatter(stream_fmtr) + + file_handler.setLevel(logging.WARNING) + self.setLevel(logging.INFO) + + self.addHandler(file_handler) + self.addHandler(stream_handler) -- Muhammad Rizki