How can I get unreal.log in Python to output while the script is running?

I have a long running script that I want to keep track of its progress, and the easiest way would be for it to use unreal.log(), but that output seems to be buffered until the python script exits. I have tried using the unreal.log_flush() function but that didn’t help.

I guess I could work around the problem by getting it to create a file and write to it, then watch it, but I’d rather just use the normal logging mechanism if I can.

For the moment I have a workaround to write to another file that I can keep an eye on:

logging.basicConfig(filename='logfile.log', filemode='w', level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', force=True)

# The level can be any of the values understood by the logging library
# note that we don't do debug since other things are using that.
def log(message, level=logging.INFO, logUnreal=True):
    logging.log(level, message)

    if logUnreal:
        if level == logging.WARNING:
            unreal.log_warning(message)
        elif level == logging.ERROR or level == logging.CRITICAL:
            unreal.log_error(message)
        else:
            unreal.log(message)

Note that this still uses block buffering so you don’t see everything at once, and to ensure that you get to see the last bit, you need to put logging.shutdown() in before you exit your script to flush the output.