from logging import Formatter, StreamHandler class NamingCache: def __init__(self): self._cache = {} def __getitem__(self, key): if key not in self._cache: self._cache[key] = self.shorten(key) return self._cache[key] @staticmethod def shorten(logger_name: str) -> str: target_length = 18 if len(logger_name) > target_length: parts = logger_name.split('.') part = 0 while len(logger_name) > target_length: if part == len(parts) - 1: logger_name = f'...{logger_name[-(target_length - 3):]}' break parts[part] = parts[part][0] logger_name = '.'.join(parts) part += 1 return logger_name.ljust(target_length) class ApplicationFormatter(Formatter): def __init__(self, handler_prefix: str = ''): super().__init__() self._logger_names = NamingCache() self._handler_prefix = handler_prefix def format(self, record): from datetime import datetime timestamp = datetime.fromtimestamp(record.created).isoformat(sep=' ', timespec='milliseconds') level = record.levelname.replace('WARNING', 'WARN').rjust(5) thread_name = record.threadName.replace(' (', '#').replace(')', '').rjust(16)[-16:] # TODO: NamingCache? logger_name = self._logger_names[f"{self._handler_prefix}{record.name}"] message = record.getMessage() formatted = f"{timestamp} {level} [{thread_name}] {logger_name} : {message}" if record.exc_info: formatted += "\n" + self.formatException(record.exc_info) return formatted class LoggingHandler(StreamHandler): def __init__(self): super().__init__() self.setFormatter(ApplicationFormatter(handler_prefix='karl.')) class ExternalLoggingHandler(StreamHandler): def __init__(self): super().__init__() self.setFormatter(ApplicationFormatter())