mirror of
https://github.com/truenas/scale-build.git
synced 2025-12-24 21:07:00 +00:00
Add logger context for checkout target
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user