logging.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 exception(msg, *args, **kwargs):
  60. """exception"""
  61. _logger.exception(msg, *args, **kwargs)
  62. def setup_logging(verbosity: str = None):
  63. """setup logging level
  64. Args:
  65. verbosity (str, optional): the logging level, `DEBUG`, `INFO`, `WARNING`. Defaults to None.
  66. """
  67. if verbosity is None:
  68. if DEBUG:
  69. verbosity = "DEBUG"
  70. else:
  71. verbosity = "INFO"
  72. if verbosity is not None:
  73. _configure_logger(_logger, verbosity.upper())
  74. def _configure_logger(logger, verbosity):
  75. """_configure_logger"""
  76. if verbosity == "DEBUG":
  77. _logger.setLevel(logging.DEBUG)
  78. elif verbosity == "INFO":
  79. _logger.setLevel(logging.INFO)
  80. elif verbosity == "WARNING":
  81. _logger.setLevel(logging.WARNING)
  82. logger.propagate = False
  83. if not logger.hasHandlers():
  84. _add_handler(logger)
  85. def _add_handler(logger):
  86. """_add_handler"""
  87. format = colorlog.ColoredFormatter(
  88. "%(log_color)s%(message)s",
  89. log_colors={key: conf["color"] for key, conf in _LOG_CONFIG.items()},
  90. )
  91. handler = logging.StreamHandler(sys.stderr)
  92. handler.setFormatter(format)
  93. logger.addHandler(handler)
  94. def advertise():
  95. """
  96. Show the advertising message like the following:
  97. ===========================================================
  98. == PaddleX is powered by PaddlePaddle ! ==
  99. ===========================================================
  100. == ==
  101. == For more info please go to the following website. ==
  102. == ==
  103. == https://github.com/PaddlePaddle/PaddleX ==
  104. ===========================================================
  105. """
  106. copyright = "PaddleX is powered by PaddlePaddle !"
  107. ad = "For more info please go to the following website."
  108. website = "https://github.com/PaddlePaddle/PaddleX"
  109. AD_LEN = 6 + len(max([copyright, ad, website], key=len))
  110. info(
  111. "\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format(
  112. "=" * (AD_LEN + 4),
  113. "=={}==".format(copyright.center(AD_LEN)),
  114. "=" * (AD_LEN + 4),
  115. "=={}==".format(" " * AD_LEN),
  116. "=={}==".format(ad.center(AD_LEN)),
  117. "=={}==".format(" " * AD_LEN),
  118. "=={}==".format(website.center(AD_LEN)),
  119. "=" * (AD_LEN + 4),
  120. )
  121. )