Skip to content

Commit

Permalink
Configure logging file handler
Browse files Browse the repository at this point in the history
  • Loading branch information
istride committed Jan 7, 2025
1 parent 17e5139 commit b3bec8e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
22 changes: 8 additions & 14 deletions src/rpft/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from collections import ChainMap


LOGGER_NAME = "main"


class LoggingContextHandler:
def __init__(self):
self.context_variables = []
Expand All @@ -26,7 +23,7 @@ def pop(self):
self.context_variables.pop()


logging_context_handler = LoggingContextHandler()
_context = LoggingContextHandler()


class logging_context:
Expand All @@ -35,29 +32,26 @@ def __init__(self, processing_unit, **kwargs):
self.kwargs = kwargs

def __enter__(self):
logging_context_handler.add(self.processing_unit, **self.kwargs)
_context.add(self.processing_unit, **self.kwargs)

def __exit__(self, exc_type, exc_value, exc_tb):
if exc_type is None:
logging_context_handler.pop()
_context.pop()


class ContextFilter(logging.Filter):

def filter(self, record):
record.processing_stack = " | ".join(
logging_context_handler.get_processing_stack()
)
record.context_variables = logging_context_handler.get_context_variables()
record.processing_stack = " | ".join(_context.get_processing_stack())
record.context_variables = _context.get_context_variables()
return True


def initialize_main_logger(file_path="errors.log"):
handler = logging.FileHandler(file_path, "w")
handler.addFilter(ContextFilter())
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s:%(name)s: %(processing_stack)s: %(message)s",
handlers=[handler],
)

for handler in logging.root.handlers:
handler.addFilter(ContextFilter())
handler.addFilter(logging.Filter("rpft.parsers"))
5 changes: 0 additions & 5 deletions src/rpft/parsers/common/sheetparser.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import copy
import logging

from rpft.parsers.common.rowdatasheet import RowDataSheet
from rpft.parsers.common.rowparser import RowParser
from rpft.logger.logger import logging_context


LOGGER = logging.getLogger(__name__)


class SheetParser:
def parse_sheet(table, row_model):
"""
Expand Down Expand Up @@ -44,7 +40,6 @@ def __init__(self, table, row_model=None, row_parser=None, context={}):
self.input_rows.append((row_dict, row_idx + 2))
self.iterator = iter(self.input_rows)
self.context = copy.deepcopy(context)
LOGGER.info("Sheet reader created")

def add_to_context(self, key, value):
self.context[key] = value
Expand Down
4 changes: 2 additions & 2 deletions src/rpft/rapidpro/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def generate_field_key(field_name):
if len(key) > FIELD_KEY_MAX_LENGTH:
raise RapidProActionError(
"Contact field key length limit exceeded",
{"length": len(key), "limit": FIELD_KEY_MAX_LENGTH, "key": key}
{"length": len(key), "limit": FIELD_KEY_MAX_LENGTH, "key": key},
)

if not re.search("[A-Za-z]", key):
raise RapidProActionError(
"Contact field key without letter characters detected",
{"key": key, "name": field_name}
{"key": key, "name": field_name},
)

return key
Expand Down

0 comments on commit b3bec8e

Please sign in to comment.