You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

2 years ago
  1. #!/usr/bin/env python
  2. # coding: utf8
  3. """ Centralized logging facilities for Spleeter. """
  4. import logging
  5. import warnings
  6. from os import environ
  7. # pyright: reportMissingImports=false
  8. # pylint: disable=import-error
  9. from typer import echo
  10. # pylint: enable=import-error
  11. __email__ = "spleeter@deezer.com"
  12. __author__ = "Deezer Research"
  13. __license__ = "MIT License"
  14. environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
  15. class TyperLoggerHandler(logging.Handler):
  16. """ A custom logger handler that use Typer echo. """
  17. def emit(self, record: logging.LogRecord) -> None:
  18. echo(self.format(record))
  19. formatter = logging.Formatter("%(levelname)s:%(name)s:%(message)s")
  20. handler = TyperLoggerHandler()
  21. handler.setFormatter(formatter)
  22. logger: logging.Logger = logging.getLogger("spleeter")
  23. logger.addHandler(handler)
  24. logger.setLevel(logging.INFO)
  25. def configure_logger(verbose: bool) -> None:
  26. """
  27. Configure application logger.
  28. Parameters:
  29. verbose (bool):
  30. `True` to use verbose logger, `False` otherwise.
  31. """
  32. from tensorflow import get_logger
  33. from tensorflow.compat.v1 import logging as tf_logging
  34. tf_logger = get_logger()
  35. tf_logger.handlers = [handler]
  36. if verbose:
  37. tf_logging.set_verbosity(tf_logging.INFO)
  38. logger.setLevel(logging.DEBUG)
  39. else:
  40. warnings.filterwarnings("ignore")
  41. tf_logging.set_verbosity(tf_logging.ERROR)