logging.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # coding=utf-8
  2. # Copyright 2020 Optuna, Hugging Face
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Logging utilities."""
  16. import logging
  17. import os
  18. from logging import (
  19. CRITICAL, # NOQA
  20. DEBUG, # NOQA
  21. ERROR, # NOQA
  22. FATAL, # NOQA
  23. INFO, # NOQA
  24. NOTSET, # NOQA
  25. WARN, # NOQA
  26. WARNING, # NOQA
  27. )
  28. from typing import Optional
  29. from .. import constants
  30. log_levels = {
  31. "debug": logging.DEBUG,
  32. "info": logging.INFO,
  33. "warning": logging.WARNING,
  34. "error": logging.ERROR,
  35. "critical": logging.CRITICAL,
  36. }
  37. _default_log_level = logging.WARNING
  38. def _get_library_name() -> str:
  39. return __name__.split(".")[0]
  40. def _get_library_root_logger() -> logging.Logger:
  41. return logging.getLogger(_get_library_name())
  42. def _get_default_logging_level():
  43. """
  44. 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
  45. - fall back to `_default_log_level`
  46. """
  47. env_level_str = os.getenv("HF_HUB_VERBOSITY", None)
  48. if env_level_str:
  49. if env_level_str in log_levels:
  50. return log_levels[env_level_str]
  51. else:
  52. logging.getLogger().warning(
  53. f"Unknown option HF_HUB_VERBOSITY={env_level_str}, has to be one of: {', '.join(log_levels.keys())}"
  54. )
  55. return _default_log_level
  56. def _configure_library_root_logger() -> None:
  57. library_root_logger = _get_library_root_logger()
  58. library_root_logger.addHandler(logging.StreamHandler())
  59. library_root_logger.setLevel(_get_default_logging_level())
  60. def _reset_library_root_logger() -> None:
  61. library_root_logger = _get_library_root_logger()
  62. library_root_logger.setLevel(logging.NOTSET)
  63. def get_logger(name: Optional[str] = None) -> logging.Logger:
  64. """
  65. Returns a logger with the specified name. This function is not supposed
  66. to be directly accessed by library users.
  67. Args:
  68. name (`str`, *optional*):
  69. The name of the logger to get, usually the filename
  70. Example:
  71. ```python
  72. >>> from huggingface_hub import get_logger
  73. >>> logger = get_logger(__file__)
  74. >>> logger.set_verbosity_info()
  75. ```
  76. """
  77. if name is None:
  78. name = _get_library_name()
  79. return logging.getLogger(name)
  80. def get_verbosity() -> int:
  81. """Return the current level for the HuggingFace Hub's root logger.
  82. Returns:
  83. Logging level, e.g., `huggingface_hub.logging.DEBUG` and
  84. `huggingface_hub.logging.INFO`.
  85. > [!TIP]
  86. > HuggingFace Hub has following logging levels:
  87. >
  88. > - `huggingface_hub.logging.CRITICAL`, `huggingface_hub.logging.FATAL`
  89. > - `huggingface_hub.logging.ERROR`
  90. > - `huggingface_hub.logging.WARNING`, `huggingface_hub.logging.WARN`
  91. > - `huggingface_hub.logging.INFO`
  92. > - `huggingface_hub.logging.DEBUG`
  93. """
  94. return _get_library_root_logger().getEffectiveLevel()
  95. def set_verbosity(verbosity: int) -> None:
  96. """
  97. Sets the level for the HuggingFace Hub's root logger.
  98. Args:
  99. verbosity (`int`):
  100. Logging level, e.g., `huggingface_hub.logging.DEBUG` and
  101. `huggingface_hub.logging.INFO`.
  102. """
  103. _get_library_root_logger().setLevel(verbosity)
  104. def set_verbosity_info():
  105. """
  106. Sets the verbosity to `logging.INFO`.
  107. """
  108. return set_verbosity(INFO)
  109. def set_verbosity_warning():
  110. """
  111. Sets the verbosity to `logging.WARNING`.
  112. """
  113. return set_verbosity(WARNING)
  114. def set_verbosity_debug():
  115. """
  116. Sets the verbosity to `logging.DEBUG`.
  117. """
  118. return set_verbosity(DEBUG)
  119. def set_verbosity_error():
  120. """
  121. Sets the verbosity to `logging.ERROR`.
  122. """
  123. return set_verbosity(ERROR)
  124. def disable_propagation() -> None:
  125. """
  126. Disable propagation of the library log outputs. Note that log propagation is
  127. disabled by default.
  128. """
  129. _get_library_root_logger().propagate = False
  130. def enable_propagation() -> None:
  131. """
  132. Enable propagation of the library log outputs. Please disable the
  133. HuggingFace Hub's default handler to prevent double logging if the root
  134. logger has been configured.
  135. """
  136. _get_library_root_logger().propagate = True
  137. _configure_library_root_logger()
  138. if constants.HF_DEBUG:
  139. # If `HF_DEBUG` environment variable is set, set the verbosity of `huggingface_hub` logger to `DEBUG`.
  140. set_verbosity_debug()