codekingpro/portable-devtools
114k
1# Copyright 2020 Optuna, Hugging Face2#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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14"""Logging utilities."""15 16import logging17import os18from logging import (19 CRITICAL, # NOQA20 DEBUG, # NOQA21 ERROR, # NOQA22 FATAL, # NOQA23 INFO, # NOQA24 NOTSET, # NOQA25 WARN, # NOQA26 WARNING, # NOQA27)28 29from .. import constants30 31 32log_levels = {33 "debug": logging.DEBUG,34 "info": logging.INFO,35 "warning": logging.WARNING,36 "error": logging.ERROR,37 "critical": logging.CRITICAL,38}39 40_default_log_level = logging.WARNING41 42 43def _get_library_name() -> str:44 return __name__.split(".")[0]45 46 47def _get_library_root_logger() -> logging.Logger:48 return logging.getLogger(_get_library_name())49 50 51def _get_default_logging_level():52 """53 If `HF_HUB_VERBOSITY` env var is set to one of the valid choices return that as the new default level. If it is not54 - fall back to `_default_log_level`55 """56 env_level_str = os.getenv("HF_HUB_VERBOSITY", None)57 if env_level_str:58 if env_level_str in log_levels:59 return log_levels[env_level_str]60 else:61 logging.getLogger().warning(62 f"Unknown option HF_HUB_VERBOSITY={env_level_str}, has to be one of: {', '.join(log_levels.keys())}"63 )64 return _default_log_level65 66 67def _configure_library_root_logger() -> None:68 library_root_logger = _get_library_root_logger()69 library_root_logger.addHandler(logging.StreamHandler())70 library_root_logger.setLevel(_get_default_logging_level())71 72 73def _reset_library_root_logger() -> None:74 library_root_logger = _get_library_root_logger()75 library_root_logger.setLevel(logging.NOTSET)76 77 78def get_logger(name: str | None = None) -> logging.Logger:79 """80 Returns a logger with the specified name. This function is not supposed81 to be directly accessed by library users.82 83 Args:84 name (`str`, *optional*):85 The name of the logger to get, usually the filename86 87 Example:88 89 ```python90 >>> from huggingface_hub import get_logger91 92 >>> logger = get_logger(__file__)93 >>> logger.set_verbosity_info()94 ```95 """96 97 if name is None:98 name = _get_library_name()99 100 return logging.getLogger(name)101 102 103def get_verbosity() -> int:104 """Return the current level for the HuggingFace Hub's root logger.105 106 Returns:107 Logging level, e.g., `huggingface_hub.logging.DEBUG` and108 `huggingface_hub.logging.INFO`.109 110 > [!TIP]111 > HuggingFace Hub has following logging levels:112 >113 > - `huggingface_hub.logging.CRITICAL`, `huggingface_hub.logging.FATAL`114 > - `huggingface_hub.logging.ERROR`115 > - `huggingface_hub.logging.WARNING`, `huggingface_hub.logging.WARN`116 > - `huggingface_hub.logging.INFO`117 > - `huggingface_hub.logging.DEBUG`118 """119 return _get_library_root_logger().getEffectiveLevel()120 121 122def set_verbosity(verbosity: int) -> None:123 """124 Sets the level for the HuggingFace Hub's root logger.125 126 Args:127 verbosity (`int`):128 Logging level, e.g., `huggingface_hub.logging.DEBUG` and129 `huggingface_hub.logging.INFO`.130 """131 _get_library_root_logger().setLevel(verbosity)132 133 134def set_verbosity_info():135 """136 Sets the verbosity to `logging.INFO`.137 """138 return set_verbosity(INFO)139 140 141def set_verbosity_warning():142 """143 Sets the verbosity to `logging.WARNING`.144 """145 return set_verbosity(WARNING)146 147 148def set_verbosity_debug():149 """150 Sets the verbosity to `logging.DEBUG`.151 """152 return set_verbosity(DEBUG)153 154 155def set_verbosity_error():156 """157 Sets the verbosity to `logging.ERROR`.158 """159 return set_verbosity(ERROR)160 161 162def disable_propagation() -> None:163 """164 Disable propagation of the library log outputs. Note that log propagation is165 disabled by default.166 """167 _get_library_root_logger().propagate = False168 169 170def enable_propagation() -> None:171 """172 Enable propagation of the library log outputs. Please disable the173 HuggingFace Hub's default handler to prevent double logging if the root174 logger has been configured.175 """176 _get_library_root_logger().propagate = True177 178 179_configure_library_root_logger()180 181if constants.HF_DEBUG:182 # If `HF_DEBUG` environment variable is set, set the verbosity of `huggingface_hub` logger to `DEBUG`.183 set_verbosity_debug()184 