Catching logger.info output in python subprocess?

Question:

hello.py:

import logging
logging.info("hello, I am a log")

wrapper.py:

import subprocess
from subprocess import PIPE

result = subprocess.run('python ./hello.py'.split(' '), stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(f'returncode: {result.returncode}')
print(f'stdout: {result.stdout}')
print(f'stderr: {result.stderr}')

When I call python wrapper.py, I expected to see this output:

returncode: 0
stdout: INFO:root:hello, I am a log
stderr: 

However, instead I get this output:

returncode: 0
stdout:
stderr: 

And, if I replace logging.info() with logger.warning(), then I get this output:

returncode: 0
stdout:
stderr: WARNING:root:hello, I am a log

But, I have a bunch of code with logging.info all over the place, and I’d like to ingest it into a python subprocess. How might I do that?


Final notes:

  • I imagine most people would prefer to call hello.py by doing import hello, but let’s say that’s not an option in this case.
  • I am using Python 3.8.
Asked By: solvingPuzzles

||

Answers:

Ok, to let this have an answered, then, for potential future readers.

Add e.g. logging.getLogger(__name__).setLevel(logging.INFO) to hello.py. The default logging level is warning, as user blues points out. Therefore when you use logging.info() that doesn’t produce any output, and this is why you do not see it.

See more at https://docs.python.org/3/library/logging.html#logging.Logger.setLevel

Answered By: petre