logging.py 3.8 KB

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