hv0905/NekoImageGallery

View on GitHub
app/util/fastapi_log_handler.py

Summary

Maintainability
A
0 mins
Test Coverage
import logging

from loguru import logger


class InterceptHandler(logging.Handler):  # pragma: no cover  Hard to test in test environments

    def emit(self, record: logging.LogRecord):
        # Get corresponding Loguru level if it exists
        try:
            level = logger.level(record.levelname).name
        except ValueError:
            level = record.levelno

        # Find caller from where originated the logged message
        frame, depth = logging.currentframe(), 2
        while frame.f_code.co_filename == logging.__file__:
            frame = frame.f_back
            depth += 1

        logger.opt(depth=depth, exception=record.exc_info).log(
            level, record.getMessage()
        )


def init_logging():
    """
    Replaces logging handlers with a handler for using the custom handler.
    """

    # disable handlers for specific uvicorn loggers
    # to redirect their output to the default uvicorn logger
    # works with uvicorn==0.11.6
    intercept_handler = InterceptHandler()
    loggers = (
        logging.getLogger(name)
        for name in logging.root.manager.loggerDict
        if name.startswith("uvicorn.")
    )
    for uvicorn_logger in loggers:
        uvicorn_logger.handlers = [intercept_handler]

    # change handler for default uvicorn logger

    # logging.getLogger("uvicorn").handlers = [intercept_handler]
    logging.getLogger("uvicorn").handlers = []