The following dead simple code:
import logging
import requests
logging.basicConfig(level=logging.DEBUG)
res = requests.get('https://www.stackoverflow.com')
Being run like python ./test.py > foo.txt sends everything to stderr. Why is this not going to stdout?
logging.basicConfig uses StreamHandler when no handler/filename/stream parameter is given, and StreamHandler defaults to the STDERR stream:
class StreamHandler(Handler):
def __init__(self, stream=None):
"""
Initialize the handler.
If stream is not specified, sys.stderr is used.
"""
Handler.__init__(self)
if stream is None:
stream = sys.stderr # <- Here
self.stream = stream
To use the STDOUT, pass sys.stdout as stream:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
Now, as you have currently, you can capture the STDERR from shell like:
python ./test.py 2> foo.txt
So redirecting the file descriptor 2 (which is STDERR) will do.
STDOUT is file descriptor 1 and when you do bare redirection >, 1> is assumed.
If for some reason you want to use different files for redirecting two streams, you can do:
python ./test.py >stdout.txt 2>stderr.txt
If you want to redirect both to the same file:
python ./test.py >all.txt 2>&1 # POSIX
or
python ./test.py &>all.txt # `bash`-ism (works in other advanced shells as well)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With