tl;dr how do I pass a logging.Logger
instance as a "stream" (similar to sys.stdout
) that logs a message for each line of text received?
I'm using the invoke
module Runner.run
method to run a program /usr/bin/printf
. The run
method has a parameter out_stream
. The docs for Runner.run
read:
run(command: str, **kwargs: Any) → Optional[invoke.runners.Result]
Execute command, returning an instance of Result once complete.
out_stream
– A file-like stream object to which the subprocess’ standard output should be written. IfNone
(the default),sys.stdout
will be used.
The call to run
looks like:
import invoke
def print_foobar(context: invoke.context):
context.run("/usr/bin/printf foobar")
I want to pass a Logger
instance as the out_stream
like this (pseudo-code):
import invoke
import logging
log = logging.getLogger()
def print_foobar(context: invoke.context):
context.run("/usr/bin/printf foobar", out_stream=log.stream)
I want the standard out of the child process printf
to be logged by Logger
instance log
. Maybe one log.info
message per line of text from the printf
child process, or something like that. In other words, how do I get something like a log.stream
shown in the prior code snippet?
How can I use the Logger
instance as a "file-like stream object" that logs a message for each line of text received?