Haozhe Xie
Haozhe Xie

Reputation: 3656

Run subprocess and nothing output to logging

Suppose that I have two Python scripts:

The first script named test.py which outputs a "Test" with logging

import logging

logging.info("Test")

The second script calls the first script with subprocess and redirects IO.

import subprocess

p = subprocess.Popen(
        ['python',  'test.py'], 
        stdout=subprocess.PIPE, 
        stderr=subprocess.STDOUT)
print(p.stdout.read())

The second script outputs nothing instead of "Test". If I replace print("Test") with logging.info("Test"), everything works fine.

How to solve this problem?

Upvotes: 1

Views: 1096

Answers (1)

han solo
han solo

Reputation: 6590

There are two things to note here:

  1. Logging level is set to WARNING by default
  2. The log is written to stderr, rather than stdout

A simple test can demonstrate this,

$ python3 -c 'import logging; logging.info("hello world")'
$
$ python3 -c 'import logging; logging.warning("hello world")' >/dev/null
WARNING:root:hello world

Also,

>>> import sys
>>> 
>>> code = """import logging
... logging.warning("hello world")
... """
>>> 
>>> import subprocess
>>> subprocess.run([sys.executable, '-c', code], stdout=subprocess.PIPE)
WARNING:root:hello world
CompletedProcess(args=['/usr/bin/python3', '-c', 'import logging\nlogging.warning("hello world")\n'], returncode=0, stdout=b'') # see stdout is empty ?
>>> subprocess.run([sys.executable, '-c', code], stderr=subprocess.PIPE)
CompletedProcess(args=['/usr/bin/python3', '-c', 'import logging\nlogging.warning("hello world")\n'], returncode=0, stderr=b'WARNING:root:hello world\n') # see stderr have the data

Note: You should probably set your logging conf properly :)

Upvotes: 3

Related Questions