Skip to main content

logindent

Documentation Python PyPI Build Status Coverage License

Lightweight utility class for structurally indenting log messages.

About

Indenting log messages is a simple way to make log messages more human readable. In particular, indenting can help distinguish messages for high-level tasks from messages for their subtasks. For example, in the following indented log:

Starting task 1
    Running step 1
    Running step 2
    Running step 3
Completed task
Starting a second task
    Querying server
    Parsing response
    Updating database
Completed second task

it is immediately clear where tasks 1 and 2 begin, and which sub-steps belong to each task.

The logindent package provides the lightweight IndentLogger class to help implement this functionality. The class provides routines to track indenting levels and record log messages to standard logging streams. When logging messages, the correct amount of indentation will be prepended to the log message automatically. Users can manage the indentation level using either context blocks, or via managed IndentLogger objects.

Examples

Indented logging using context blocks:

from logindent import IndentLogger

logger = IndentLogger("my-logger")
logger.info("Starting task 1")
with logger.indent():
    logger.debug("Running step 1")
    logger.debug("Running step 2")
    logger.debug("Running step 3")
    with logger.indent():
        logger.debug("Ran sub-step A")
        logger.debug("Ran sub-step B")
logger.info("Completed task 1")
INFO  - Starting task 1
DEBUG -     Running step 1
DEBUG -     Running step 2
DEBUG -     Running step 3
DEBUG -         Ran sub-step A
DEBUG -         Ran sub-step B
INFO  - Completed task 1

Indented logging using managed loggers:

logger.info("Starting task 1")

task_logger = logger.indented()
task_logger.debug("Running step 1")
task_logger.debug("Running step 2")
task_logger.debug("Running step 3")

step3_logger = task_logger.indented()
step3_logger.debug("Ran sub-step A")
step3_logger.debug("Ran sub-step B")

logger.info("Completed task 1")
INFO  - Starting task 1
DEBUG -     Running step 1
DEBUG -     Running step 2
DEBUG -     Running step 3
DEBUG -         Ran sub-step A
DEBUG -         Ran sub-step B
INFO  - Completed task 1

Customize indent level and string:

logger = IndentLogger('my-logger', indent_level=2, indent_string="..")

logger.info("Starting task 1 in some running process")
with logger.indent():
    logger.debug("Running step 1")
    logger.debug("Running step 2")
    logger.debug("Running step 3")
INFO  - ....Starting task 1 in some running process
DEBUG - ......Running step 1
DEBUG - ......Running step 2
DEBUG - ......Running step 3

When to use IndentLogger

The IndentLogger class is best used when you want a lightweight, text-based solution to make logs more human-readable. In particular, IndentLogger works well for both console and file logs. By contrast, other solutions for human-readable logs (such as colorized messages or rich trees) can sometimes render poorly when logged to file.

Finally, if you want logs that are machine readable (rather than human readable), then consider using structured logging instead of IndentLogger.

Release files for logindent 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logindent 1.1.0
File Size Uploaded
logindent-1.1.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logindent 1.1.0
File Interpreter ABI Platform
logindent-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / logindent-1.1.0.tar.gz

Download URL logindent-1.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
212387c017196b3dcb9ac33e3b5950c7bc1d3f58b8ebc1d5f06493e12a577e95
BLAKE2b-256 checksum
How to use checksums
41c4507dbd8ebd582995273af58db9a242788009b293d3dade5942e3dd93e3c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / logindent-1.1.0-py3-none-any.whl

Download URL logindent-1.1.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7344f5ef0fba8424f5dc78b09b59698f9861e933fba197d44062d1e1fbeecf98
BLAKE2b-256 checksum
How to use checksums
5b83e7d135927b218c20e9a04d9f9beb296cafc35f9240bfd45110a61ebd052e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page