logging.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright 2020 Optuna, Hugging Face
  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. """Logging utilities."""
  15. import logging
  16. import os
  17. from logging import (
  18. CRITICAL, # NOQA
  19. DEBUG, # NOQA
  20. ERROR, # NOQA
  21. FATAL, # NOQA
  22. INFO, # NOQA
  23. NOTSET, # NOQA
  24. WARN, # NOQA
  25. WARNING, # NOQA
  26. )
  27. from .. import constants
  28. log_levels = {
  29. "debug": logging.DEBUG,
  30. "info": logging.INFO,
  31. "warning": logging.WARNING,
  32. "error": logging.ERROR,
  33. "critical": logging.CRITICAL,
  34. }
  35. _default_log_level = logging.WARNING
  36. def _get_library_name() -> str:
  37. return __name__.split(".")[0]
  38. def _get_library_root_logger() -> logging.Logger:
  39. return logging.getLogger(_get_library_name())
  40. def _get_default_logging_level():
  41. """
  42. If `HF_HUB_VERBOSITY` env var is set to one of the valid choices return that as the new default level. If it is not
  43. - fall back to `_default_log_level`
  44. """
  45. env_level_str = os.getenv("HF_HUB_VERBOSITY", None)
  46. if env_level_str:
  47. if env_level_str in log_levels:
  48. return log_levels[env_level_str]
  49. else:
  50. logging.getLogger().warning(
  51. f"Unknown option HF_HUB_VERBOSITY={env_level_str}, has to be one of: {', '.join(log_levels.keys())}"
  52. )
  53. return _default_log_level
  54. def _configure_library_root_logger() -> None:
  55. library_root_logger = _get_library_root_logger()
  56. library_root_logger.addHandler(logging.StreamHandler())
  57. library_root_logger.setLevel(_get_default_logging_level())
  58. def _reset_library_root_logger() -> None:
  59. library_root_logger = _get_library_root_logger()
  60. library_root_logger.setLevel(logging.NOTSET)
  61. def get_logger(name: str | None = None) -> logging.Logger:
  62. """
  63. Returns a logger with the specified name. This function is not supposed
  64. to be directly accessed by library users.
  65. Args:
  66. name (`str`, *optional*):
  67. The name of the logger to get, usually the filename
  68. Example:
  69. ```python
  70. >>> from huggingface_hub import get_logger
  71. >>> logger = get_logger(__file__)
  72. >>> logger.set_verbosity_info()
  73. ```
  74. """
  75. if name is None:
  76. name = _get_library_name()
  77. return logging.getLogger(name)
  78. def get_verbosity() -> int:
  79. """Return the current level for the HuggingFace Hub's root logger.
  80. Returns:
  81. Logging level, e.g., `huggingface_hub.logging.DEBUG` and
  82. `huggingface_hub.logging.INFO`.
  83. > [!TIP]
  84. > HuggingFace Hub has following logging levels:
  85. >
  86. > - `huggingface_hub.logging.CRITICAL`, `huggingface_hub.logging.FATAL`
  87. > - `huggingface_hub.logging.ERROR`
  88. > - `huggingface_hub.logging.WARNING`, `huggingface_hub.logging.WARN`
  89. > - `huggingface_hub.logging.INFO`
  90. > - `huggingface_hub.logging.DEBUG`
  91. """
  92. return _get_library_root_logger().getEffectiveLevel()
  93. def set_verbosity(verbosity: int) -> None:
  94. """
  95. Sets the level for the HuggingFace Hub's root logger.
  96. Args:
  97. verbosity (`int`):
  98. Logging level, e.g., `huggingface_hub.logging.DEBUG` and
  99. `huggingface_hub.logging.INFO`.
  100. """
  101. _get_library_root_logger().setLevel(verbosity)
  102. def set_verbosity_info():
  103. """
  104. Sets the verbosity to `logging.INFO`.
  105. """
  106. return set_verbosity(INFO)
  107. def set_verbosity_warning():
  108. """
  109. Sets the verbosity to `logging.WARNING`.
  110. """
  111. return set_verbosity(WARNING)
  112. def set_verbosity_debug():
  113. """
  114. Sets the verbosity to `logging.DEBUG`.
  115. """
  116. return set_verbosity(DEBUG)
  117. def set_verbosity_error():
  118. """
  119. Sets the verbosity to `logging.ERROR`.
  120. """
  121. return set_verbosity(ERROR)
  122. def disable_propagation() -> None:
  123. """
  124. Disable propagation of the library log outputs. Note that log propagation is
  125. disabled by default.
  126. """
  127. _get_library_root_logger().propagate = False
  128. def enable_propagation() -> None:
  129. """
  130. Enable propagation of the library log outputs. Please disable the
  131. HuggingFace Hub's default handler to prevent double logging if the root
  132. logger has been configured.
  133. """
  134. _get_library_root_logger().propagate = True
  135. _configure_library_root_logger()
  136. if constants.HF_DEBUG:
  137. # If `HF_DEBUG` environment variable is set, set the verbosity of `huggingface_hub` logger to `DEBUG`.
  138. set_verbosity_debug()