logging.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import sys
  16. import colorlog
  17. from .flags import DEBUG
  18. __all__ = ["debug", "info", "warning", "error", "critical", "setup_logging"]
  19. LOGGER_NAME = "paddlex"
  20. _LOG_CONFIG = {
  21. "DEBUG": {"color": "purple"},
  22. "INFO": {"color": "green"},
  23. "WARNING": {"color": "yellow"},
  24. "ERROR": {"color": "red"},
  25. "CRITICAL": {"color": "bold_red"},
  26. }
  27. _logger = logging.getLogger(LOGGER_NAME)
  28. def debug(msg, *args, **kwargs):
  29. """debug"""
  30. _logger.debug(msg, *args, **kwargs)
  31. def info(msg, *args, **kwargs):
  32. """info"""
  33. _logger.info(msg, *args, **kwargs)
  34. def warning(msg, *args, **kwargs):
  35. """warning"""
  36. _logger.warning(msg, *args, **kwargs)
  37. def error(msg, *args, **kwargs):
  38. """error"""
  39. _logger.error(msg, *args, **kwargs)
  40. def critical(msg, *args, **kwargs):
  41. """critical"""
  42. _logger.critical(msg, *args, **kwargs)
  43. def setup_logging(verbosity: str = None):
  44. """setup logging level
  45. Args:
  46. verbosity (str, optional): the logging level, `DEBUG`, `INFO`, `WARNING`. Defaults to None.
  47. """
  48. if verbosity is None:
  49. if DEBUG:
  50. verbosity = "DEBUG"
  51. else:
  52. verbosity = "INFO"
  53. if verbosity is not None:
  54. _configure_logger(_logger, verbosity.upper())
  55. def _configure_logger(logger, verbosity):
  56. """_configure_logger"""
  57. if verbosity == "DEBUG":
  58. _logger.setLevel(logging.DEBUG)
  59. elif verbosity == "INFO":
  60. _logger.setLevel(logging.INFO)
  61. elif verbosity == "WARNING":
  62. _logger.setLevel(logging.WARNING)
  63. logger.propagate = False
  64. if not logger.hasHandlers():
  65. _add_handler(logger)
  66. def _add_handler(logger):
  67. """_add_handler"""
  68. format = colorlog.ColoredFormatter(
  69. "%(log_color)s%(message)s",
  70. log_colors={key: conf["color"] for key, conf in _LOG_CONFIG.items()},
  71. )
  72. handler = logging.StreamHandler(sys.stderr)
  73. handler.setFormatter(format)
  74. logger.addHandler(handler)
  75. def advertise():
  76. """
  77. Show the advertising message like the following:
  78. ===========================================================
  79. == PaddleX is powered by PaddlePaddle ! ==
  80. ===========================================================
  81. == ==
  82. == For more info please go to the following website. ==
  83. == ==
  84. == https://github.com/PaddlePaddle/PaddleX ==
  85. ===========================================================
  86. """
  87. copyright = "PaddleX is powered by PaddlePaddle !"
  88. ad = "For more info please go to the following website."
  89. website = "https://github.com/PaddlePaddle/PaddleX"
  90. AD_LEN = 6 + len(max([copyright, ad, website], key=len))
  91. info(
  92. "\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format(
  93. "=" * (AD_LEN + 4),
  94. "=={}==".format(copyright.center(AD_LEN)),
  95. "=" * (AD_LEN + 4),
  96. "=={}==".format(" " * AD_LEN),
  97. "=={}==".format(ad.center(AD_LEN)),
  98. "=={}==".format(" " * AD_LEN),
  99. "=={}==".format(website.center(AD_LEN)),
  100. "=" * (AD_LEN + 4),
  101. )
  102. )