Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to disable logging from imported modules in Python?
When working with Python applications, imported modules often generate unwanted log messages that can clutter your output. The logging module provides several methods to disable or control logging from specific imported modules.
Understanding Logger Hierarchy
Python's logging system uses a hierarchical structure where loggers are organized by name. When you import a module, it may create its own logger that inherits from the root logger. You can control these loggers individually using their names.
Method 1: Using setLevel() with getLogger()
The most common approach is to set the logging level for specific modules to a higher threshold, effectively filtering out lower-priority messages ?
import logging
import requests # This module produces many INFO messages
# Set up basic logging
logging.basicConfig(level=logging.INFO)
# Disable INFO messages from requests module
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
# Your application logging will still work
logging.info("This message will appear")
INFO:root:This message will appear
Method 2: Using disabled Flag
You can completely disable a logger by setting its disabled attribute to True ?
import logging
# Set up basic logging
logging.basicConfig(level=logging.INFO)
# Completely disable logging from specific modules
logging.getLogger("requests").disabled = True
logging.getLogger("urllib3").disabled = True
logging.info("Application logging still works")
INFO:root:Application logging still works
Method 3: Using NullHandler
Add a NullHandler to suppress all messages from a specific logger ?
import logging
# Set up basic logging
logging.basicConfig(level=logging.INFO)
# Add NullHandler to suppress module logging
module_logger = logging.getLogger("noisy_module")
module_logger.addHandler(logging.NullHandler())
module_logger.propagate = False
logging.info("Main application message")
INFO:root:Main application message
Practical Example: Disabling Third-Party Library Logs
import logging
# Configure your application logging
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s'
)
# Disable verbose logging from common third-party libraries
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("matplotlib").setLevel(logging.WARNING)
# Your application logs will still appear
logger = logging.getLogger(__name__)
logger.info("Application started successfully")
INFO:__main__:Application started successfully
Comparison of Methods
| Method | Effect | Best For |
|---|---|---|
setLevel(WARNING) |
Filters by priority | Keeping errors visible |
disabled = True |
Completely silences logger | Total suppression |
NullHandler |
Absorbs all messages | Fine-grained control |
Conclusion
Use setLevel(logging.WARNING) to filter out debug and info messages while preserving errors. For complete suppression, set disabled = True on the specific logger. Choose the method based on whether you want to preserve error messages from imported modules.
