Skip to main content

Like `subprocess.run`, but with the ability to pipe `stdout` and/or `stderr` to a `Logger` or capture each stream independently while the process is running.

Project description

run_with_logger

Like subprocess.run, but with the ability to pipe stdout and/or stderr to a Logger or capture each stream independently while the process is running.

Examples

Log stderr and capture stdout

The main process will wait for the subprocess while:

  • forwarding stderr line by line to the logger
  • capturing stdout in a CompletedProcess object, which is returned when the process finishes
from run_with_logger import run_with_logger
from logging import getLogger

logger = getLogger(__name__)

completed = run_with_logger(
    args=['foo', 'bar'],
    logger=logger,
    stdout_action="capture",
    stderr_action="log",
    check=False,
)

print(completed.stdout)

Log stdout and discard stderr

The main process will wait for the subprocess while:

  • forwarding stdout line by line to the logger
  • discarding stderr
from run_with_logger import run_with_logger
from logging import getLogger

logger = getLogger(__name__)

completed = run_with_logger(
    args=['foo', 'bar'],
    logger=logger,
    stdout_action="log",
    stderr_action="discard",
    check=False,
)

print(completed.stdout)

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

run_with_logger-1.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

run_with_logger-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file run_with_logger-1.0.0.tar.gz.

File metadata

  • Download URL: run_with_logger-1.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for run_with_logger-1.0.0.tar.gz
Algorithm Hash digest
SHA256 76e140d94c627831e2f7833fd4e142781b74dc31bc6990c526477a47f1171d1b
MD5 9d9fd64a0629ac115f4d622d26418055
BLAKE2b-256 2f99acd713939ebc2ab631570cf2396507755408fa0445649a46812b8cf8f708

See more details on using hashes here.

File details

Details for the file run_with_logger-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for run_with_logger-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 729b9cc61cc910a8174682a06abd22753f264c24e544192d3a92c405dd34acdd
MD5 f61e2b79823e313ab617e928e720e1fe
BLAKE2b-256 2c94dba4099a4fc111e86299b60877ff2b6066ce50532c3fa9273f0cdfd3cd55

See more details on using hashes here.

Supported by

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