Move Custom Formatter class for logger to util.helper module from main.py

This commit is contained in:
Debanjum Singh Solanky 2022-10-20 00:26:23 +05:30
parent 5ed17ccbd7
commit 55d2fea9be
3 changed files with 26 additions and 28 deletions

View file

@ -1,8 +1,6 @@
# System Packages # Standard Packages
import sys import sys
import logging import logging
# External Packages
import json import json
# Internal Packages # Internal Packages

View file

@ -24,6 +24,7 @@ from src.routers.api_beta import api_beta
from src.routers.web_client import web_client from src.routers.web_client import web_client
from src.utils import constants, state from src.utils import constants, state
from src.utils.cli import cli from src.utils.cli import cli
from src.utils.helpers import CustomFormatter
from src.interface.desktop.main_window import MainWindow from src.interface.desktop.main_window import MainWindow
from src.interface.desktop.system_tray import create_system_tray from src.interface.desktop.system_tray import create_system_tray
@ -38,31 +39,6 @@ app.include_router(web_client)
logger = logging.getLogger('src') 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(): def run():
# Turn Tokenizers Parallelism Off. App does not support it. # Turn Tokenizers Parallelism Off. App does not support it.
os.environ["TOKENIZERS_PARALLELISM"] = 'false' os.environ["TOKENIZERS_PARALLELISM"] = 'false'

View file

@ -4,6 +4,7 @@ import sys
from os.path import join from os.path import join
from collections import OrderedDict from collections import OrderedDict
from typing import Optional, Union from typing import Optional, Union
import logging
def is_none_or_empty(item): def is_none_or_empty(item):
@ -81,3 +82,26 @@ class LRU(OrderedDict):
oldest = next(iter(self)) oldest = next(iter(self))
del self[oldest] 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)