Tiered Debug Logging Helper

1.4.0 · active · verified Fri Apr 17

Tiered Debug is a Python logging helper module that provides multiple, configurable levels of debug logging. It wraps standard Python `logging.debug()` calls, allowing developers to set a maximum debug tier that will be logged at runtime. The library is actively maintained, with version 1.4.0 released recently, and shows a consistent release cadence with several updates in the past year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `TieredDebug`, set its debug level, and log messages using the `lvX()` methods or the generic `log()` method. It also shows how to temporarily adjust the logging level using the `log_level_context` context manager and how to pass standard `logging` keyword arguments like `exc_info`.

import logging
import os
from tiered_debug import TieredDebug

# Configure a basic logger for demonstration
logging.basicConfig(
    level=logging.DEBUG,
    format='%(levelname)s:%(name)s:%(message)s'
)

# Instantiate TieredDebug with a specific level or rely on default (1)
# The debug_level argument overrides any TIERED_DEBUG_LEVEL env var for this instance.
td = TieredDebug(debug_level=3, logger_name="my_app")

# Log at different tiers
td.lv1("This is a level 1 debug message. Always visible if debug_level >= 1.")
td.lv2("This is a level 2 debug message. Visible if debug_level >= 2.")
td.lv3("This is a level 3 debug message. Visible if debug_level >= 3.")
td.lv4("This is a level 4 debug message. Not visible with current config (debug_level=3).")

# Using the generic log method
td.log(5, "This message is also at level 5. Not visible.")

# Temporarily increase the debug level using a context manager
print("\n--- Entering higher debug context ---")
with td.log_level_context(5):
    td.lv4("This level 4 message is now visible within the context!")
    td.lv5("And this level 5 message is also visible!")
    # You can also pass keyword arguments directly to the underlying logging call
    try:
        raise ValueError("Something went wrong!")
    except ValueError:
        td.lv1("Error with exc_info", exc_info=True)
print("--- Exiting higher debug context ---")

td.lv4("This level 4 message is no longer visible after exiting the context.")

view raw JSON →