logging.py 858 B

12345678910111213141516171819202122232425262728293031
  1. import logging
  2. import os
  3. from typing import Union
  4. from rich.console import Console
  5. from rich.logging import RichHandler
  6. def setup_logging(
  7. terminal_width: Union[int, None] = None, level: Union[int, None] = None
  8. ) -> None:
  9. if level is None:
  10. level = (
  11. logging.DEBUG if os.getenv("FASTAPI_CLOUD_DEBUG") == "1" else logging.INFO
  12. )
  13. logger = logging.getLogger("fastapi_cloud_cli")
  14. console = Console(width=terminal_width) if terminal_width else None
  15. rich_handler = RichHandler(
  16. show_time=False,
  17. rich_tracebacks=True,
  18. tracebacks_show_locals=True,
  19. markup=True,
  20. show_path=False,
  21. console=console,
  22. )
  23. rich_handler.setFormatter(logging.Formatter("{message}", style="{"))
  24. logger.addHandler(rich_handler)
  25. logger.setLevel(level)
  26. logger.propagate = False