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
loggingmodule. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file subprocess_logger-0.3.0.tar.gz.
File metadata
- Download URL: subprocess_logger-0.3.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
348639f5429642f28cf5652f210e9f66719aa0c0d2d13ad2d688fd84a94ae734
|
|
| MD5 |
48cb1c700cdd0b7bf234a4246da99683
|
|
| BLAKE2b-256 |
b557084e1afd45d16f9c5ae7de3cd52e8b79dacdb45f10155020df217c35db3d
|
File details
Details for the file subprocess_logger-0.3.0-py3-none-any.whl.
File metadata
- Download URL: subprocess_logger-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03a1fdd2cd985fcfba810b915bcdf77f3b938eec4f6c12efbbb42335c21e312c
|
|
| MD5 |
d393f662ba98b0378668a3cc94044fdc
|
|
| BLAKE2b-256 |
49cd5995d7a71145ec8b342af1c6be0c06067d98f673f3c11cccde3adcd14b93
|