"""Set some (hopefully) sane and comfortable logging defaults.""" import logging def _setup() -> logging.Logger: import logging import os import setuptools import glob # Determine log levels project_level = getattr(logging, os.environ.get("LOG_LEVEL", "INFO")) root_level = getattr(logging, os.environ.get("ROOT_LOG_LEVEL", "WARNING")) root_level = max(project_level, root_level) # Determine projects project_names = ( ["__main__", "repl"] + [x[:-3] for x in glob.glob("*.py")] + setuptools.find_packages() ) # Set logging levels logging.root.setLevel(root_level) for name in project_names: logging.getLogger(name).setLevel(project_level) # Configure logging format try: from colorlog import ColoredFormatter except ModuleNotFoundError: formatter = logging.Formatter( "\u001b[36m%(asctime)s " "\u001b[0m%(levelname)-8s " "\u001b[32m%(threadName)s " "\u001b[36m%(name)s " "\u001b[37;1m%(message)s" "\u001b[0m", datefmt="%H:%M", ) else: class FancyFormatter(logging.Formatter): """Formatter that uses a different formatter for debug logs.""" def __init__( self, formatter: logging.Formatter, debug_formatter: logging.Formatter, ) -> None: super().__init__() self.formatter = formatter self.debug_formatter = debug_formatter def format( self, record: logging.LogRecord, ) -> str: if record.levelno <= logging.DEBUG: return self.debug_formatter.format(record) return self.formatter.format(record) fmt = ( "%(cyan)s%(asctime)s " "%(log_color)s%(levelname)-8s " "%(green)s%(threadName)s " "%(cyan)s%(name)s " "%(white)s%(message)s" ) formatter = FancyFormatter( formatter=ColoredFormatter( fmt=fmt, datefmt="%H:%M", reset=True, log_colors={ "DEBUG": "white", "INFO": "white", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bold_red", }, ), debug_formatter=ColoredFormatter( fmt=f"%(thin)s{fmt}", datefmt="%H:%M", reset=True, log_colors={ "DEBUG": "white", "INFO": "white", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bold_red", }, ), ) # Create root handler with formatter handler = logging.StreamHandler() handler.setFormatter(formatter) if not logging.root.handlers: logging.root.addHandler(handler) return logging.getLogger("repl") logger = _setup() del _setup _summarise_startup()