Skip to main content

Run a process and log stdin and stdout.

Project description

logproc

Run a process and log stdin and stdout.

Description

This experimental module offers a function execute() that runs a commandline in a subprocess and separately live captures the called process’s standard output and standard error. By default, the output is logged at different levels, but it is possible to provide a callback for different handling.

Usage

In the simplest case, just run execute(["ls", "-l"]), which will run ls -l and log its output with Python’s default logging system, using the logger id ls and the level INFO for stdout, WARNING for stderr.

If you need further customization, here are the two API functions of this module:

execute

def execute(cmd: Sequence[str],
            stdout: OutputHandler | None = None,
            stdout_level: int = logging.INFO,
            stderr: OutputHandler | None = None,
            stderr_level: int = logging.WARNING,
            cwd=None) -> int

Run the given command and log its output as it appears.

Arguments

  • cmd - A list of arguments, as in subprocess.run etc.

  • stdout, stderr: Handlers for the specified stream. Each of these can be:

    • None (the default) to use the default settings
    • a logger name
    • a logging.Logger
    • a callback

    You can use proc_logger to build a suitable callback.

  • stdout_level, stderr_level: Logging levels for the specific output.

Returns

the command’s exit code

Description

execute runs the given command and waits for it to finish. While it is running, its stdout and stderr streams are monitored. Each new line appearing on these streams are immediately handled.

The default handlers will log the message from the subprocess to a logger that logs using the first member of the cmd sequence as a logger name and logging.INFO for stdout and logging.WARNING for stderr output.

proc_logger

def proc_logger(prefix: str = "",
                level: int = logging.INFO,
                logger: LoggerSpec = None,
                extra=None) -> OutputCallback

Creates a callback for execute() that logs to a logger.

Arguments

  • prefix - String that will be prepended to each line
  • logger - If given, this is either a logger or the name of a logger. If missing, we log to the root logger.
  • level - The level at which to log the messages.
  • extra - an optional dictionary with extra attributes that gets past on with each log entry

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

logproc-0.2.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

logproc-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: logproc-0.2.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for logproc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 674091abc9eebae3d0273118f8d4592fda255b67545fa13461777c5d11f1fc71
MD5 1c824d4335a9d33cc72553db07745613
BLAKE2b-256 914505f4047cf400e007cd658522794a0c1d6f0ef73e17792e91635fa1e25774

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logproc-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for logproc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 322f6c522611b6b3b183908ff3979de651d0fa191da0ed1c7ad7684a170b8331
MD5 a79327b49e563f5714db4afc6b53815c
BLAKE2b-256 ce5c7fb47071a502ea244f745dd0ff8c0bec1d6749cc7d17391d1c5629e0c80d

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