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