From 55d2fea9becd62f6a2d6b3150f68a8c46d33a7c6 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Thu, 20 Oct 2022 00:26:23 +0530 Subject: [PATCH] Move Custom Formatter class for logger to util.helper module from main.py --- src/configure.py | 4 +--- src/main.py | 26 +------------------------- src/utils/helpers.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/configure.py b/src/configure.py index 495ac313..f38b7d11 100644 --- a/src/configure.py +++ b/src/configure.py @@ -1,8 +1,6 @@ -# System Packages +# Standard Packages import sys import logging - -# External Packages import json # Internal Packages diff --git a/src/main.py b/src/main.py index 4f522572..6b81bfba 100644 --- a/src/main.py +++ b/src/main.py @@ -24,6 +24,7 @@ from src.routers.api_beta import api_beta from src.routers.web_client import web_client from src.utils import constants, state from src.utils.cli import cli +from src.utils.helpers import CustomFormatter from src.interface.desktop.main_window import MainWindow from src.interface.desktop.system_tray import create_system_tray @@ -38,31 +39,6 @@ app.include_router(web_client) logger = logging.getLogger('src') -class CustomFormatter(logging.Formatter): - - blue = "\x1b[1;34m" - green = "\x1b[1;32m" - grey = "\x1b[38;20m" - yellow = "\x1b[33;20m" - red = "\x1b[31;20m" - bold_red = "\x1b[31;1m" - reset = "\x1b[0m" - format_str = "%(levelname)s: %(asctime)s: %(name)s | %(message)s" - - FORMATS = { - logging.DEBUG: blue + format_str + reset, - logging.INFO: green + format_str + reset, - logging.WARNING: yellow + format_str + reset, - logging.ERROR: red + format_str + reset, - logging.CRITICAL: bold_red + format_str + reset - } - - def format(self, record): - log_fmt = self.FORMATS.get(record.levelno) - formatter = logging.Formatter(log_fmt) - return formatter.format(record) - - def run(): # Turn Tokenizers Parallelism Off. App does not support it. os.environ["TOKENIZERS_PARALLELISM"] = 'false' diff --git a/src/utils/helpers.py b/src/utils/helpers.py index 8425a8fa..2285018d 100644 --- a/src/utils/helpers.py +++ b/src/utils/helpers.py @@ -4,6 +4,7 @@ import sys from os.path import join from collections import OrderedDict from typing import Optional, Union +import logging def is_none_or_empty(item): @@ -81,3 +82,26 @@ class LRU(OrderedDict): oldest = next(iter(self)) del self[oldest] + +class CustomFormatter(logging.Formatter): + blue = "\x1b[1;34m" + green = "\x1b[1;32m" + grey = "\x1b[38;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format_str = "%(levelname)s: %(asctime)s: %(name)s | %(message)s" + + FORMATS = { + logging.DEBUG: blue + format_str + reset, + logging.INFO: green + format_str + reset, + logging.WARNING: yellow + format_str + reset, + logging.ERROR: red + format_str + reset, + logging.CRITICAL: bold_red + format_str + reset + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + return formatter.format(record) \ No newline at end of file