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.2.0.tar.gz (5.1 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.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: subprocess_logger-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 a7b6f603d369ae40a316aa4f652a5096d9fb7fb3b7c447bfccc90822a143adb4
MD5 1447064a2f3fd521f9342e615b4591f8
BLAKE2b-256 1629a8477309dbca5f93db5bbf08faa4f7fd52b4285ccf0e81ebf2a6eec9f3a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for subprocess_logger-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d590c303bcc718373df0033b5e3c89c28861b92fa488fccb3285e094fd19023
MD5 5c9778f82957981a8ca9b7a27613ff8e
BLAKE2b-256 b7024cce65b93acf867c99f2b5248ad35d23b47f92e56958e8c82a13076107cd

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