#!/usr/bin/env python3 # Standard library imports import logging import logging.config import logging.handlers import os import sys from traceback import print_exception import yaml # PyQt imports # Third party imports import stackprinter # type: ignore # App imports from config import Config class FunctionFilter(logging.Filter): """Filter to allow category-based logging to stderr.""" def __init__(self, functions: set[str]): super().__init__() self.functions = functions def filter(self, record: logging.LogRecord) -> bool: return ( getattr(record, "funcName", None) in self.functions and getattr(record, "levelname", None) == "DEBUG" ) class LevelTagFilter(logging.Filter): """Add leveltag""" def filter(self, record: logging.LogRecord) -> bool: # Extract the first character of the level name record.leveltag = record.levelname[0] # We never actually filter messages out, just add an extra field # to the LogRecord return True # Load YAML logging configuration with open("app/logging.yaml", "r") as f: config = yaml.safe_load(f) logging.config.dictConfig(config) # Get logger log = logging.getLogger(Config.LOG_NAME) def log_uncaught_exceptions(type_, value, traceback): from helpers import send_mail print("\033[1;31;47m") print_exception(type_, value, traceback) print("\033[1;37;40m") print( stackprinter.format( value, suppressed_paths=["/pypoetry/virtualenvs/"], style="darkbg" ) ) if os.environ["MM_ENV"] == "PRODUCTION": msg = stackprinter.format(value) send_mail( Config.ERRORS_TO, Config.ERRORS_FROM, "Exception (log_uncaught_exceptions) from musicmuster", msg, ) log.debug(msg) sys.excepthook = log_uncaught_exceptions