Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3: logging.basicConfig sends everything to stderr?

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?

like image 296
Wells Avatar asked Nov 01 '25 03:11

Wells


1 Answers

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)
like image 131
heemayl Avatar answered Nov 02 '25 16:11

heemayl