Skip to content

Commit

Permalink
Merge pull request #113 from Hazannovich/improve_logging
Browse files Browse the repository at this point in the history
Debug: improve logging information and formatter.
  • Loading branch information
srbhr authored Aug 19, 2023
2 parents 14a141f + ba7523e commit 88dd59c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions run_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from scripts.utils.ReadFiles import get_filenames_from_dir
from scripts.ResumeProcessor import ResumeProcessor
from scripts.JobDescriptionProcessor import JobDescriptionProcessor
from scripts.utils.logger import init_logging_config
import logging
import os
logging.basicConfig(filename='app.log', filemode='w',
level=logging.DEBUG,
format='%(name)s - %(levelname)s - %(message)s')

init_logging_config()

PROCESSED_RESUMES_PATH = "Data/Processed/Resumes"
PROCESSED_JOB_DESCRIPTIONS_PATH = "Data/Processed/JobDescription"
Expand Down
41 changes: 41 additions & 0 deletions scripts/utils/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging


def init_logging_config():
class CustomFormatter(logging.Formatter):
def __init__(self, file=False):
super().__init__()
yellow = "\x1b[36;10m" if not file else ""
blue = "\x1b[35;10m" if not file else ""
green = "\x1b[32;10m" if not file else ""
red = "\x1b[31;10m" if not file else ""
bold_red = "\x1b[31;1m" if not file else ""
reset = "\x1b[0m" if not file else ""
log = "%(asctime)s (%(filename)s:%(lineno)d) - %(levelname)s: "
msg = reset + "%(message)s"

self.FORMATS = {
logging.DEBUG: blue + log + msg,
logging.INFO: green + log + msg,
logging.WARNING: yellow + log + msg,
logging.ERROR: red + log + msg,
logging.CRITICAL: bold_red + log + msg,
}

def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.DEBUG)
stderr_handler.setFormatter(CustomFormatter())
logger.addHandler(stderr_handler)

file_handler = logging.FileHandler("app.log", mode="w")
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(CustomFormatter(True))
logger.addHandler(file_handler)

0 comments on commit 88dd59c

Please sign in to comment.