log.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """ Logging utilities.
  2. """
  3. import logging
  4. import logging.handlers
  5. from colorama import Fore, Style
  6. COLORS = {
  7. 'DEBUG': Style.DIM,
  8. 'INFO': Style.NORMAL,
  9. 'WARNING': Style.BRIGHT,
  10. 'ERROR': Fore.RED,
  11. 'CRITICAL': Style.BRIGHT + Fore.RED,
  12. }
  13. class ColoredFormatter(logging.Formatter):
  14. def format(self, record):
  15. return COLORS[record.levelname] + logging.Formatter.format(self, record) + Style.RESET_ALL
  16. def setup_logging(verbose=True, color=True):
  17. """ Sets logging format. """
  18. logging.getLogger().setLevel(logging.DEBUG)
  19. stream = logging.StreamHandler()
  20. stream.setLevel(logging.DEBUG if verbose else logging.INFO)
  21. if color:
  22. stream_format = ColoredFormatter(
  23. "%(asctime)s %(name)s %(levelname)s %(message)s"
  24. )
  25. else:
  26. stream_format = logging.Formatter(
  27. "%(asctime)s %(name)s %(levelname)s %(message)s"
  28. )
  29. stream.setFormatter(stream_format)
  30. logging.getLogger().addHandler(stream)
  31. logging.getLogger('requests').setLevel(logging.ERROR)