Python console interference between logging streamHandler() and input()

  console, input, logging, python

I’m new to python. I’ve encountered a minor nuisance in my console output when using a streamHandler() output from the logging module together with an input() line.

I configure my logger with the following function:

def configure_logger(filename='log'):
    """Configures a logger which outputs at level INFO and above to both
    the console and a file called 'filename'.
    file_handler = logging.FileHandler(filename, mode='w')
    stream_handler = logging.StreamHandler()
    file_formatter = logging.Formatter(datefmt="%d/%m/%Y %H:%M:%S",
                                       fmt='%(levelname)-8s %(asctime)s '
                                           '%(name)-50s - %(message)s')
    stream_formatter = logging.Formatter(fmt='%(levelname)-8s %(name)-50s - '
    logging.basicConfig(handlers=(file_handler, stream_handler),

Later I call the following function twice in a row to create two directories:

def create_directory(directory):
    """Creates a directory, prompting the user to confirm overwrite if
    it already exists
    logger = logging.getLogger(f'{__name__}.create_directory')'Creating directory {directory}')
    except FileExistsError:
        overwrite = input(f'Directory {directory} already '
                          f'exists. Overwrite? (y/n): ')
        while overwrite not in ['y', 'yes', 'n', 'no']:
            overwrite = input(f'Enter yes/y or no/n?: ')
        if overwrite in ['y', 'yes']:
            logger.warning('Overwriting existing directory')

If the directory I am trying to create already exists, I prompt the user and ask them to confirm they want to overwrite it using the input() function.
Following a ‘yes’ response, I log a warning ‘Overwriting existing directory’.

After calling create_directory() twice in a row, i get the following in my console:

INFO     sowfatools.create_directory                        - Creating directory time_histories
Directory time_histories already exists. Overwrite? (y/n): >? y
Directory time_history_plots already exists. Overwrite? (y/n): WARNING  sowfatools.create_directory                        - Overwriting existing directory
INFO     sowfatools.create_directory                        - Creating directory time_history_plots


The second input line (which comes from the second function call) is printed to the console BEFORE the warning from the first function call. In fact it comes BEFORE the first logging message in the second call as well.

Why might this be? and how do I fix it? It doesn’t make my code unusable, but it is a nuisance. Is the logging streamhandler just too slow to keep up with the rest of the code?
Also, as I’m new to this, any other feedback on how I’m doing things is welcome.


Source: Python Questions