How to synchronize logging with printing in Python?

Viewed 1815

The following code

 logging.info('Collecting available features for location ' + source_feature['properties']['key'] + ' and date range [' + start_date + '..' + end_date + ']...')

    feature_start_time = datetime.now()

    target_directory = util.target_directory(source_feature['properties'])
    if target_directory is None:
        target_directory = util.target_directory_for_point(point)

    try:

        for layer in layers:

            logging.info('Layer: \'' + layer + '\'')

            print('Found dates: ', end='', flush=True)
            criterion_date = end_date
            while criterion_date is not None:

prints something like this

Found dates: 20170630130831 Collecting available features for location 8404222738792 and date range [2016-04-16..2017-06-30]...
20170630130831 Layer: '1_NATURAL_COL0R'
2017-06-22 2017-05-13 2017-04-23 2017-04-03 2017-03-14 2017-02-22 2017-01-23 2016-11-24 2016-10-05 2016-08-06 2016-07-17 2016-06-27 2016-06-07 

i.e. print outputs interleaved with logger outputs.

How to synchronize? I wan't logs printed in the same moment as they called and doesn't interleave with print's.

UPDATE

Logging initialized with following

import logging

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y%m%d%H%M%S', level=logging.INFO)
3 Answers

I ran into the same problem. By default StreamHandler log all data into sys.stderr. And print prints data to sys.stdout. So your program is syncronized, but your terminal doesnt. Try to manually create StreamHandler and pass stream=sys.stdout into constructor. After that your output should be passed to stdout and all will be in sync.

Try setting the stream to sys.stdout in your basicConfing() call:

logging.basicConfig(stream=sys.stdout, format='%(asctime)s %(message)s', datefmt='%Y%m%d%H%M%S', level=logging.INFO)

or print to sys.stderr stream:

print('Found dates: ', end='', flush=True, file=sys.stderr)
Related