Dr. Fabien Tarrade
Dr. Fabien Tarrade

Reputation: 1706

How to redirect abseil logging messages to stout instead of stderr?

I am using python 3.7.6. and abseil module for logging messages with absl-py 0.9.0. I am using this piece of code for my tests.

from absl import logging
from absl import app

def main(argv):

    #logging.set_stderrthreshold(logging.ERROR)
    #logging._warn_preinit_stderr = False
    logging.set_verbosity(logging.DEBUG)

    print(' 0 -----')
    logging.debug(' 1 logging-debug-test')
    logging.info(' 2 logging-info-test')
    logging.warning(' 3 logging-warning-test')
    logging.error('4 logging-error-test')
    print(' 5 -----')

if __name__ == '__main__':
    app.run(main)

When testing it in a Jupyter notebook, it is clear from the color code of the background that abseil messages are in the stderr stream.

enter image description here

Same things when executing the python code in a shell: enter image description here enter image description here
I tried few things with different values like:

logging.set_stderrthreshold(logging.DEBUG)
logging._warn_preinit_stderr = True

but I still see 100% the same output.

Upvotes: 1

Views: 4898

Answers (1)

Dr. Fabien Tarrade
Dr. Fabien Tarrade

Reputation: 1706

I was told that this is the standard behavior and what Python's standard logging module does. In my case adding the following line redirect the logging messages to stdout:

logging.get_absl_handler().python_handler.stream = sys.stdout

Now in my Jupyter notebook it looks like that: enter image description here

Upvotes: 2

Related Questions