[Python] How is the logging module still outputting even when stdout and stderr are set to...

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    This is a simple proof-of-concept script

    import logging


    logging.basicConfig(level=logging.DEBUG)

    logging.info('This is a test for info')
    logging.error('This is a test for error')
    print('This is a test for print')

    sys.__stdout__ = None
    sys.stdout = None

    sys.__stderr__ = None
    sys.stderr = None

    logging.info('This is a test for info after setting stdout to None')
    logging.error('This is a test for error after setting stderr to None')
    print('This is a test for print after setting stdout to None')


    And the output is:

    INFO:root:This is a test for info
    ERROR:root:This is a test for error
    This is a test for print
    INFO:root:This is a test for info after setting stdout to None
    ERROR:root:This is a test for error after setting stderr to None


    As you can see the print statement is not outputting the string after setting stdout/stderr to None.

    But how is the logging module still outputting the strings into the terminal, is there a reason behind this?

    Continue reading...

Compartilhe esta Página