#
Logging

🤗 Transformers拥有一个集中式的日志系统，因此您可以轻松设置库输出的日志详细程度。

当前库的默认日志详细程度为`WARNING`。

要更改日志详细程度，只需使用其中一个直接的setter。例如，以下是如何将日志详细程度更改为INFO级别的方法：

    import transformers

    transformers.logging.set_verbosity_info()

您还可以使用环境变量`TRANSFORMERS_VERBOSITY`来覆盖默认的日志详细程度。您可以将其设置为以下级别之一：`debug`、`info`、`warning`、`error`、`critical`。例如：

    TRANSFORMERS_VERBOSITY=error ./myprogram.py

此外，通过将环境变量`TRANSFORMERS_NO_ADVISORY_WARNINGS`设置为`true`（如1），可以禁用一些`warnings`。这将禁用`logger.warning_advice`记录的任何警告。例如：

    TRANSFORMERS_NO_ADVISORY_WARNINGS=1 ./myprogram.py

以下是如何在您自己的模块或脚本中使用与库相同的logger的示例：

    from transformers.utils import logging

    logging.set_verbosity_info()
    logger = logging.get_logger("transformers")
    logger.info("INFO")
    logger.warning("WARN")

此日志模块的所有方法都在下面进行了记录，主要的方法包括 logging.get_verbosity() 用于获取logger当前输出日志详细程度的级别和 logging.set_verbosity() 用于将详细程度设置为您选择的级别。按照顺序（从最不详细到最详细），这些级别（及其相应的整数值）为：

- `transformers.logging.CRITICAL` 或 `transformers.logging.FATAL`（整数值，50）：仅报告最关键的errors。
- `transformers.logging.ERROR`（整数值，40）：仅报告errors。
- `transformers.logging.WARNING` 或 `transformers.logging.WARN`（整数值，30）：仅报告error和warnings。这是库使用的默认级别。
- `transformers.logging.INFO`（整数值，20）：报告error、warnings和基本信息。
- `transformers.logging.DEBUG`（整数值，10）：报告所有信息。

默认情况下，将在模型下载期间显示`tqdm`进度条。logging.disable_progress_bar() 和 logging.enable_progress_bar() 可用于禁止或启用此行为。

##
logging vs warnings

Python有两个经常一起使用的日志系统：如上所述的`logging`，和对特定buckets中的警告进行进一步分类的`warnings`，例如，`FutureWarning`用于输出已经被弃用的功能或路径，`DeprecationWarning`用于指示即将被弃用的内容。

我们在`transformers`库中同时使用这两个系统。我们利用并调整了`logging`的`captureWarning`方法，以便通过上面的详细程度setters来管理这些警告消息。

对于库的开发人员，这意味着什么呢？我们应该遵循以下启发法则：

- 库的开发人员和依赖于`transformers`的库应优先使用`warnings`
- `logging`应该用于在日常项目中经常使用它的用户

以下是`captureWarnings`方法的参考。

####
transformers.utils.logging.captureWarnings

< source >

( capture )

Calls the `captureWarnings` method from the logging library to enable management of the warnings emitted by the `warnings` library.

Read more about this method here: https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module

All warnings will be logged through the `py.warnings` logger.

Careful: this method also adds a handler to this logger if it does not already have one, and updates the logging level of that logger to the library’s root logger.

##
Base setters

####
transformers.utils.logging.set_verbosity_error

< source >

( )

Set the verbosity to the `ERROR` level.

####
transformers.utils.logging.set_verbosity_warning

< source >

( )

Set the verbosity to the `WARNING` level.

####
transformers.utils.logging.set_verbosity_info

< source >

( )

Set the verbosity to the `INFO` level.

####
transformers.utils.logging.set_verbosity_debug

< source >

( )

Set the verbosity to the `DEBUG` level.

##
Other functions

####
transformers.utils.logging.get_verbosity

< source >

( ) →
`int`

Returns

`int`

The logging level.

Return the current level for the 🤗 Transformers’s root logger as an int.

🤗 Transformers has following logging levels:

- 50: `transformers.logging.CRITICAL` or `transformers.logging.FATAL`
- 40: `transformers.logging.ERROR`
- 30: `transformers.logging.WARNING` or `transformers.logging.WARN`
- 20: `transformers.logging.INFO`
- 10: `transformers.logging.DEBUG`

####
transformers.utils.logging.set_verbosity

< source >

( verbosity: int )

Parameters

- verbosity (`int`) — Logging level, e.g., one of:
  - `transformers.logging.CRITICAL` or `transformers.logging.FATAL`
  - `transformers.logging.ERROR`
  - `transformers.logging.WARNING` or `transformers.logging.WARN`
  - `transformers.logging.INFO`
  - `transformers.logging.DEBUG`

Set the verbosity level for the 🤗 Transformers’s root logger.

####
transformers.utils.logging.get_logger

< source >

( name: Optional = None )

Return a logger with the specified name.

This function is not supposed to be directly accessed unless you are writing a custom transformers module.

####
transformers.utils.logging.enable_default_handler

< source >

( )

Enable the default handler of the HuggingFace Transformers’s root logger.

####
transformers.utils.logging.disable_default_handler

< source >

( )

Disable the default handler of the HuggingFace Transformers’s root logger.

####
transformers.utils.logging.enable_explicit_format

< source >

( )

Enable explicit formatting for every HuggingFace Transformers’s logger. The explicit formatter is as follows:

        [LEVELNAME|FILENAME|LINE NUMBER] TIME >> MESSAGE

All handlers currently bound to the root logger are affected by this method.

####
transformers.utils.logging.reset_format

< source >

( )

Resets the formatting for HuggingFace Transformers’s loggers.

All handlers currently bound to the root logger are affected by this method.

####
transformers.utils.logging.enable_progress_bar

< source >

( )

Enable tqdm progress bar.

####
transformers.utils.logging.disable_progress_bar

< source >

( )

Disable tqdm progress bar.

< > Update on GitHub