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 kanazawa.. (unknown [101.128.126.198]) by gnuweeb.org (Postfix) with ESMTPSA id 1FAD980632; Fri, 4 Nov 2022 18:09:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667585395; bh=/JVpiRkO6heX2kLHoInb8NHNZ7mP44LFSarydnDh3C4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X6JeCusWe/GOwcN9aQnkrmc5FFKQtlkl9ixHkox5v4m7JmeiL3n6YzMxqUZ+Mz0JD VP0cTdzmp45ztR4XFqOCvu/j6F0FLXdRGaM1JyaGAAJqQNJ7+4foCb2v0dKnYHAGp8 Qvu2tKRurmL6CLCrA9Mv1vErEkpQsCiYBsLv5ScA+Y75+6PxQhBGFSGGN/aF4LbDFP DNehgNgMUEwC7NuhdtJS39vO2+qq7PlPP90LGUveN+OLHq2ucGvC/e8S3gLXeHsazo hSU80I0zeTxEjEJNCcE1h0DNM3G+3jMXCj/84ro+ng+JG7TQWiqp6/PYMK1rsqJ9bm 55Ul1enu+Kx6Q== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 07/16] logger: Initial work for the bot logger for future use Date: Sat, 5 Nov 2022 01:09:14 +0700 Message-Id: <20221104180931.3852-8-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221104180931.3852-1-kiizuha@gnuweeb.org> References: <20221104180931.3852-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