Let’s say I have a logging setup like this in my Python script:
import logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
logging.info('info')
logging.error('error...')
logging.debug('debug...')
Is there a way I can have it wait to print to stdout until the script is finished running and sort the log messages by level before printing?
From the looks of it, the object passed to
streamonly needs to have awritemethod. This means you can create a list-like object which appends data whenwriteis called — You can then sort the list-like object trivially before printing.Of course, you might need to make your sort key a little more to pick up different levels. Something like: