Logging levels determine the severity or importance of the messages that are logged. They help in filtering the logs based on the desired granularity. The transformers library defines several logging levels, each serving a specific purpose.
1. DEBUG
Description: Detailed information, typically of interest only when diagnosing problems.
Use Case: Fine-grained informational events useful for debugging an application.
Example: Logging every step of the model loading process.
2. INFO
Description: Confirmation that things are working as expected.
Use Case: General events that confirm the application is functioning correctly.
Example: Indicating successful completion of a model download.
3. WARNING
Description: An indication that something unexpected happened, or indicative of some problem in the near future (e.g., "disk space low"). The software is still working as expected.
Use Case: Non-critical issues that may require attention but do not halt the application's operation.
Example: Vocab size mismatches between the model and tokenizer.
4. ERROR
Description: Due to a more serious problem, the software has not been able to perform some function.
Use Case: Critical issues that prevent parts of the application from functioning.
Example: Failure to load a model due to missing files or authentication errors.
5. CRITICAL
Description: A serious error, indicating that the program itself may be unable to continue running.
Use Case: Severe issues that require immediate attention.
Example: Complete failure to initialise necessary resources, leading to application shutdown.
Configuring logging levels in Transformers
The transformers library provides a straightforward way to configure logging verbosity using its logging module. Here's how you can set and customise logging levels:
1. Import the Logging Module
First, import the logging module from transformers:
from transformers import logging as transformers_logging
2. Set the Desired Logging Level
Use the set_verbosity_* methods to set the logging level:
Set to DEBUG
transformers_logging.set_verbosity_debug()
Enables detailed debugging information.
Set to INFO
transformers_logging.set_verbosity_info()
Enables general informational messages.
Set to WARNING
transformers_logging.set_verbosity_warning()
Enables warnings about potential issues.
Set to ERROR
transformers_logging.set_verbosity_error()
Enables error messages indicating failed operations.
Set to CRITICAL
transformers_logging.set_verbosity_critical()
Enables critical error messages that may halt the application.
Set to NOTSET
transformers_logging.set_verbosity_notset()
Resets logging to the default level (usually WARNING).
Customising logging levels
If the predefined set_verbosity_* methods do not meet your specific requirements, you can customise the logging level using the set_verbosity method by passing the desired level directly.
from transformers import logging as transformers_logging
import logging
# Set logging to a custom level
transformers_logging.set_verbosity(logging.WARNING)
Supported Logging Levels
logging.NOTSET: No specific logging level
logging.DEBUG: Detailed debugging information
logging.INFO: Informational messages
logging.WARNING: Indications of potential issues
logging.ERROR: Error messages indicating failures
logging.CRITICAL: Severe error messages indicating critical failures
Best practices
- Use Appropriate Logging Levels:
- Development: Set to DEBUG to capture detailed information.
- Production: Set to INFO or WARNING to reduce log verbosity and focus on essential information.
- Avoid Excessive Logging in Production:
- Excessive DEBUG logs can clutter log files and impact performance.
- Use higher logging levels like INFO or WARNING to maintain clarity and efficiency.
- Secure Sensitive Information:
- Be cautious about logging sensitive data. Avoid including secrets, tokens, or personal information in logs.
- Monitor Logs Regularly:
- Implement log monitoring and alerting systems to detect and respond to issues promptly.
- Customise Logs as Needed:
- Tailor logging configurations to fit the specific needs of different environments (development, staging, production).
The wrap
Understanding and configuring logging levels in the transformers library empowers you to monitor your application's behavior effectively. By setting the appropriate logging level, you can balance the need for detailed debugging information with the desire to maintain clean and manageable log files.
Quick Reference
Logging Level | Description | Method to Set |
---|---|---|
DEBUG | Detailed debugging information. | set_verbosity_debug() |
INFO | Informational messages confirming normal operations. | set_verbosity_info() |
WARNING | Indicators of potential issues or non-critical problems. | set_verbosity_warning() |
ERROR | Error messages indicating failed operations. | set_verbosity_error() |
CRITICAL | Severe error messages indicating critical failures. | set_verbosity_critical() |
NOTSET | No specific logging level. | set_verbosity_notset() |