Add logger context for checkout target

This commit is contained in:
Waqar Ahmed
2021-05-05 00:33:30 +05:00
committed by Waqar Ahmed
parent 4249e4e37c
commit 77033c3cb8
7 changed files with 83 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
import collections
import logging
import os
import threading
from .paths import LOG_DIR
@@ -11,3 +13,44 @@ def get_logger(logger_name, logger_path, mode='a+'):
logger.handlers = []
logger.addHandler(logging.FileHandler(os.path.join(LOG_DIR, logger_path), mode))
return logger
class LoggingContext:
CONTEXTS = collections.defaultdict(list)
def __init__(self, path, mode='a+'):
self.path = f'{path}.log'
self.mode = mode
def __enter__(self):
self.CONTEXTS[threading.currentThread().name].append(
logging.FileHandler(os.path.join(LOG_DIR, self.path), self.mode, delay=True)
)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.CONTEXTS[threading.currentThread().name].pop()
@staticmethod
def has_handler():
return bool(LoggingContext.CONTEXTS[threading.current_thread().name])
@staticmethod
def handler():
return LoggingContext.CONTEXTS[threading.current_thread().name][-1]
class ConsoleFilter(logging.Filter):
def filter(self, record):
return not LoggingContext.has_handler()
class LogHandler(logging.NullHandler):
def handle(self, record):
rv = LoggingContext.has_handler()
if rv:
return LoggingContext.handler().handle(record)
return rv