logging.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 inspect
  15. import logging
  16. import sys
  17. import colorlog
  18. from .flags import DEBUG
  19. __all__ = ["debug", "info", "warning", "error", "critical", "setup_logging"]
  20. LOGGER_NAME = "paddlex"
  21. _LOG_CONFIG = {
  22. "DEBUG": {"color": "purple"},
  23. "INFO": {"color": "green"},
  24. "WARNING": {"color": "yellow"},
  25. "ERROR": {"color": "red"},
  26. "CRITICAL": {"color": "bold_red"},
  27. }
  28. _logger = logging.getLogger(LOGGER_NAME)
  29. def debug(msg, *args, **kwargs):
  30. """debug"""
  31. if DEBUG:
  32. frame = inspect.currentframe()
  33. caller_frame = frame.f_back
  34. caller_func_name = caller_frame.f_code.co_name
  35. if "self" in caller_frame.f_locals:
  36. caller_class_name = caller_frame.f_locals["self"].__class__.__name__
  37. elif "cls" in caller_frame.f_locals:
  38. caller_class_name = caller_frame.f_locals["cls"].__name__
  39. else:
  40. caller_class_name = None
  41. if caller_class_name:
  42. caller_info = f"{caller_class_name}::{caller_func_name}"
  43. else:
  44. caller_info = f"{caller_func_name}"
  45. msg = f"【{caller_info}】{msg}"
  46. _logger.debug(msg, *args, **kwargs)
  47. def info(msg, *args, **kwargs):
  48. """info"""
  49. _logger.info(msg, *args, **kwargs)
  50. def warning(msg, *args, **kwargs):
  51. """warning"""
  52. _logger.warning(msg, *args, **kwargs)
  53. def error(msg, *args, **kwargs):
  54. """error"""
  55. _logger.error(msg, *args, **kwargs)
  56. def critical(msg, *args, **kwargs):
  57. """critical"""
  58. _logger.critical(msg, *args, **kwargs)
  59. def setup_logging(verbosity: str = None):
  60. """setup logging level
  61. Args:
  62. verbosity (str, optional): the logging level, `DEBUG`, `INFO`, `WARNING`. Defaults to None.
  63. """
  64. if verbosity is None:
  65. if DEBUG:
  66. verbosity = "DEBUG"
  67. else:
  68. verbosity = "INFO"
  69. if verbosity is not None:
  70. _configure_logger(_logger, verbosity.upper())
  71. def _configure_logger(logger, verbosity):
  72. """_configure_logger"""
  73. if verbosity == "DEBUG":
  74. _logger.setLevel(logging.DEBUG)
  75. elif verbosity == "INFO":
  76. _logger.setLevel(logging.INFO)
  77. elif verbosity == "WARNING":
  78. _logger.setLevel(logging.WARNING)
  79. logger.propagate = False
  80. if not logger.hasHandlers():
  81. _add_handler(logger)
  82. def _add_handler(logger):
  83. """_add_handler"""
  84. format = colorlog.ColoredFormatter(
  85. "%(log_color)s%(message)s",
  86. log_colors={key: conf["color"] for key, conf in _LOG_CONFIG.items()},
  87. )
  88. handler = logging.StreamHandler(sys.stderr)
  89. handler.setFormatter(format)
  90. logger.addHandler(handler)
  91. def advertise():
  92. """
  93. Show the advertising message like the following:
  94. ===========================================================
  95. == PaddleX is powered by PaddlePaddle ! ==
  96. ===========================================================
  97. == ==
  98. == For more info please go to the following website. ==
  99. == ==
  100. == https://github.com/PaddlePaddle/PaddleX ==
  101. ===========================================================
  102. """
  103. copyright = "PaddleX is powered by PaddlePaddle !"
  104. ad = "For more info please go to the following website."
  105. website = "https://github.com/PaddlePaddle/PaddleX"
  106. AD_LEN = 6 + len(max([copyright, ad, website], key=len))
  107. info(
  108. "\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format(
  109. "=" * (AD_LEN + 4),
  110. "=={}==".format(copyright.center(AD_LEN)),
  111. "=" * (AD_LEN + 4),
  112. "=={}==".format(" " * AD_LEN),
  113. "=={}==".format(ad.center(AD_LEN)),
  114. "=={}==".format(" " * AD_LEN),
  115. "=={}==".format(website.center(AD_LEN)),
  116. "=" * (AD_LEN + 4),
  117. )
  118. )