Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug: improve logging information and formatter. #113

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)