|
import logging |
|
import sys |
|
from typing import Union |
|
|
|
from loguru import logger |
|
|
|
from settings import app_settings |
|
|
|
|
|
class InterceptHandler(logging.Handler): |
|
""" |
|
Default handler from examples in loguru documentation. |
|
|
|
This handler intercepts all log requests and |
|
passes them to loguru. |
|
|
|
For more info see: |
|
https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging |
|
""" |
|
|
|
def emit(self, record: logging.LogRecord) -> None: |
|
"""Propagates logs to loguru. |
|
|
|
Parameters |
|
---------- |
|
record |
|
record to log. |
|
""" |
|
try: |
|
level: Union[str, int] = logger.level(record.levelname).name |
|
except ValueError: |
|
level = record.levelno |
|
|
|
|
|
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 configure_logging() -> None: |
|
"""Configures logging.""" |
|
intercept_handler = InterceptHandler() |
|
|
|
logging.basicConfig(handlers=[intercept_handler], level=logging.NOTSET) |
|
|
|
for logger_name in logging.root.manager.loggerDict: |
|
if logger_name.startswith("uvicorn."): |
|
logging.getLogger(logger_name).handlers = [] |
|
|
|
|
|
logging.getLogger("uvicorn").handlers = [intercept_handler] |
|
logging.getLogger("uvicorn.access").handlers = [intercept_handler] |
|
|
|
|
|
logger.remove() |
|
logger.add( |
|
sys.stdout, |
|
level=app_settings.log_level, |
|
) |
|
|