diff --git a/src/huggingface_hub/README.md b/src/huggingface_hub/README.md index 050b553f91..a2237e03e4 100644 --- a/src/huggingface_hub/README.md +++ b/src/huggingface_hub/README.md @@ -371,4 +371,38 @@ API. ```python inference = InferenceApi("bert-base-uncased", task="feature-extraction", token=API_TOKEN) -``` \ No newline at end of file +``` + +## Controlling the logging of `huggingface_hub` + +The `huggingface_hub` package exposes a `logging` utility to control the logging level of the package itself. +You can import it as such: + +```py +from huggingface_hub import logging +``` + +Then, you may define the verbosity in order to update the amount of logs you'll see: + +```python +from huggingface_hub import logging + +logging.set_verbosity_error() +logging.set_verbosity_warning() +logging.set_verbosity_info() +logging.set_verbosity_debug() + +logging.set_verbosity(...) +``` + +The levels should be understood as follows: + +- `error`: this will only show critical logs about usage which may result in an error or unexpected behavior. +- `warning`: this will show logs which aren't critical, about usage which may result in unintended behavior. + Additionally, important informative logs may be shown. +- `info`: this will show most logs, including some verbose logging regarding what is happening under the hood. + If something is behaving in an unexpected manner, we recommend switching the verbosity level to this in order + to get more information. +- `debug`: this shows all logs, including some internal logs which may be used to track exactly what's happening + under the hood. + diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index ea140e11ac..c53938db86 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import logging import os import subprocess import sys @@ -31,6 +30,7 @@ REPO_TYPES_URL_PREFIXES, SPACES_SDK_TYPES, ) +from .utils import logging from .utils.endpoint_helpers import ( AttributeDictionary, DatasetFilter, @@ -49,6 +49,8 @@ USERNAME_PLACEHOLDER = "hf_user" +logger = logging.get_logger(__name__) + def repo_type_and_id_from_hf_id(hf_id: str): """ @@ -422,8 +424,9 @@ def login(self, username: str, password: str) -> str: Throws: requests.exceptions.HTTPError if credentials are invalid """ - logging.error( - "HfApi.login: This method is deprecated in favor of `set_access_token`." + warnings.warn( + "HfApi.login: This method is deprecated in favor of `set_access_token` and will be removed in v0.7.", + FutureWarning, ) path = f"{self.endpoint}/api/login" r = requests.post(path, json={"username": username, "password": password}) @@ -492,7 +495,10 @@ def logout(self, token: Optional[str] = None) -> None: token (``str``, `optional`): Hugging Face token. Will default to the locally saved token if not provided. """ - logging.error("This method is deprecated in favor of `unset_access_token`.") + warnings.warn( + "HfApi.logout: This method is deprecated in favor of `unset_access_token` and will be removed in v0.7.", + FutureWarning, + ) if token is None: token = HfFolder.get_token() if token is None: @@ -1265,7 +1271,7 @@ def move_repo( ) else: raise e - logging.info( + logger.info( "Accepted transfer request. You will get an email once this is successfully completed." ) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 155200b4eb..19e71acaa1 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -1,5 +1,4 @@ import json -import logging import os from pathlib import Path from shutil import copytree, rmtree @@ -16,9 +15,10 @@ from .constants import CONFIG_NAME from .hf_api import HfApi, HfFolder from .repository import Repository +from .utils import logging -logger = logging.getLogger(__name__) +logger = logging.get_logger(__name__) if is_tf_available(): import tensorflow as tf diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 507b675411..77d2619ff1 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -139,6 +139,25 @@ def test_login_cli(self): read_from_credential_store(USERNAME_PLACEHOLDER), (None, None) ) + def test_login_deprecation_error(self): + with pytest.warns( + FutureWarning, + match=r"HfApi.login: This method is deprecated in favor of " + r"`set_access_token` and will be removed in v0.7.", + ): + self._api.login(username=USER, password=PASS) + + def test_logout_deprecation_error(self): + with pytest.warns( + FutureWarning, + match=r"HfApi.logout: This method is deprecated in favor of " + r"`unset_access_token` and will be removed in v0.7.", + ): + try: + self._api.logout() + except HTTPError: + pass + class HfApiCommonTestWithLogin(HfApiCommonTest): @classmethod