Skip to main content

Consolidated handler for subprocess logging

Project description

subprocess-logger

Consolidated handler for subprocess logging in Python.

Overview

subprocess-logger provides a unified way to capture and forward output from Python subprocesses to the standard logging system. It is useful for applications that spawn subprocesses and want to collect their stdout and stderr streams in a thread-safe, structured manner.

Features

  • Collects stdout and stderr from subprocesses and logs them using Python's logging module.
  • Allows log level customization at global and per process level for stdout and stderr.
  • Thread-safe collection and dispatching of logs.
  • Easy integration with existing logging configurations.

Installation

pip install subprocess-logger

Usage

Below is a basic usage example:

import logging
import subprocess
from subprocess_logger import install

# Configure root logger to print to console
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s %(message)s")

# Create a collector instance
collector = install(stdout_level=logging.INFO, stderr_level=logging.WARNING)

# Start a subprocess that prints to stdout and stderr
proc = subprocess.Popen(
    ["python", "-c", "import sys; print('hello from stdout'); sys.stderr.write('hello from stderr\\n')"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

# Attach the subprocess to the collector
collector.attach(proc, logger_name="demo_subprocess")

# Wait for the subprocess to finish
proc.wait()

# Stop the collector (waits for all logs to be processed)
collector.stop()

Testing

To run unit tests, use the following command:

pytest

Or, if you have tox installed:

tox

License

This project is licensed under the MIT License.

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

subprocess_logger-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

subprocess_logger-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: subprocess_logger-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for subprocess_logger-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e7dd64912090e7e74dfc703f16e1c4ee008eb6f559c709f539a7d0d49cc78ba
MD5 fb683de683aa644b0c9617b05e2c23ec
BLAKE2b-256 e5996c90ff1eab72c91bc152b0e3f69eb468371267d55089443d26695aa9f14b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for subprocess_logger-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a150bdc5ab0391b4c08b6683a6b0ba99d7e9191d5a62e8a9e46c2f19aae1f7cc
MD5 ebe4ac1071fb9323aac39548407d25b9
BLAKE2b-256 f1fe8b4e57f3d1da0b420a8e6011314080a0c0329954c6bade9d7d8439192edd

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