Skip to main content

Utility class for indenting log messages

Project description

indent-logger

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 indent_logger 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 indent_logger 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logindent-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logindent-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file logindent-0.1.0.tar.gz.

File metadata

  • Download URL: logindent-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for logindent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d85cc4b664367b42318e4efe66fd166ee45fe23a3fa4e872abe3480f2f46e98b
MD5 029086bd48882972e6a2706b6721453b
BLAKE2b-256 cdddbf4a5e1792b2a96744ed9e34b77d9d6e57988f870a718d4db147c7270eb4

See more details on using hashes here.

File details

Details for the file logindent-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: logindent-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for logindent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6e09de440dc6e2391981be7655945d6279e7871f93b10ed82fc371966513c6b
MD5 6f6fc4cbd59628258e3a17bff3dc77d2
BLAKE2b-256 ff6873dab2670bc748286b6910a796c1648a2c0c1606bbed93f991edeacf1c47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page