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.1.6.tar.gz (3.4 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.1.6-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: logproc-0.1.6.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for logproc-0.1.6.tar.gz
Algorithm Hash digest
SHA256 6c9433734c447e25628a1038cedb5a70cb9431caf654be752d9f89d5ffdad91c
MD5 80c1a58c046f219e751819a56c495a72
BLAKE2b-256 5b06615dbbfd6ac05836fcd1faabc0f72bd865c10c50b4604b4cd8314a65f1a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logproc-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for logproc-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ed9ec921d9fc493436162905cd17b75aaf6903f9ca8d9d0cfd52eca6c031671b
MD5 b84b6dd3487a73aedd526d71f0808837
BLAKE2b-256 5615c32352ef5fdbbf8cde77eca7d424b0fc56b1ffb9f255061c89d08831d007

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