Python
Logging
Programming
Code Management
Software Development

Python Logging - Disable logging from imported modules

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

If imported modules are polluting your logs, the fix is usually to configure their loggers explicitly rather than disabling Python logging globally. The clean approach is to identify the noisy logger names, raise their level, stop their propagation, or attach a NullHandler depending on how much output you want to suppress.

Understand the Logger Hierarchy First

Python loggers are hierarchical. A library often logs under names such as urllib3, botocore, or matplotlib, and those log records propagate upward to the root logger unless configured otherwise.

That means you usually do not need to modify the library code. You only need to configure the right logger name in your application.

Suppress One Specific Imported Module

To silence a known noisy module, get its logger and disable it:

python
import logging

logging.getLogger("urllib3").disabled = True

This is blunt but effective. A more flexible option is to keep only warnings or errors:

python
import logging

logging.getLogger("botocore").setLevel(logging.WARNING)

That suppresses low-value debug and info output while preserving higher-severity events.

Prevent Propagation to the Root Logger

Some libraries attach their own handlers or emit records that bubble up to your root logger. In that case, disable propagation:

python
1import logging
2
3third_party_logger = logging.getLogger("noisy_package")
4third_party_logger.propagate = False
5third_party_logger.handlers.clear()
6third_party_logger.addHandler(logging.NullHandler())

This pattern is useful when a library insists on creating records but you do not want them reaching your application handlers.

Configure Logging Centrally

For larger projects, dictConfig is cleaner than scattered one-off logger tweaks.

python
1import logging.config
2
3logging.config.dictConfig({
4    "version": 1,
5    "disable_existing_loggers": False,
6    "formatters": {
7        "standard": {
8            "format": "%(asctime)s %(name)s %(levelname)s %(message)s"
9        }
10    },
11    "handlers": {
12        "console": {
13            "class": "logging.StreamHandler",
14            "formatter": "standard"
15        }
16    },
17    "root": {
18        "level": "INFO",
19        "handlers": ["console"]
20    },
21    "loggers": {
22        "urllib3": {
23            "level": "ERROR",
24            "propagate": False
25        },
26        "botocore": {
27            "level": "WARNING",
28            "propagate": False
29        }
30    }
31})

This keeps your application logging policy in one place and makes it obvious which third-party modules are being quieted.

Avoid Global Shutdown Unless You Mean It

Python also has a global switch:

python
import logging

logging.disable(logging.CRITICAL)

That disables all logging at CRITICAL and below for the entire process. It is almost never what you want in an application, because it silences your own logs too. Use it only for very controlled scripting or tests where total silence is intentional.

Order of Configuration Matters

Configure logging early in process startup. If imported modules emit logs during import time or initialize their handlers before your app config runs, you may still see unwanted output. In those cases, move your logging setup to the very beginning of program startup.

If a library writes directly with print instead of logging, none of these logger settings will help. That is a different problem and must be handled separately.

Common Pitfalls

  • Muting the root logger when the real problem is only one noisy imported library.
  • Guessing the library logger name instead of confirming it from actual log output.
  • Disabling a dependency completely and then losing warnings that would have been operationally useful.
  • Configuring suppression too late, after the imported module already emitted startup logs.
  • Setting disable_existing_loggers too aggressively in dictConfig and silencing more than intended.

Summary

  • Silence imported modules by configuring their specific logger names.
  • Prefer setLevel, propagate = False, or NullHandler over disabling all logging globally.
  • Use dictConfig for centralized, repeatable logging policy.
  • Configure logging early so third-party modules do not emit before your setup runs.
  • If output comes from print rather than logging, logger settings will not affect it.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.