Python logger inserting null (^@) control characters into file

898 Views Asked by At

I am reading some data from gpsd and writing it using using Python's logging module. I am fairly confident that only one process writes to this file, although I read from it using tail while this logger is running. Every once in a while I am seeing some log entries that look like the image below. I am hoping that somebody can shed some light on what would cause (presumably Python) to insert null control characters into my log file.

enter image description here

The code I am using is:

"""
Read the GPS continuously
"""
import logging
from logging.handlers import RotatingFileHandler
import sys
import gps

LOG = '/sensor_logs/COUNT.csv'

def main():
    log_formatter = logging.Formatter('%(asctime)s,%(message)s', "%Y-%m-%d %H:%M:%S")

    my_handler = RotatingFileHandler(LOG, mode='a', maxBytes=1024*1024,
                                     backupCount=1, encoding=None, delay=0)
    my_handler.setFormatter(log_formatter)
    my_handler.setLevel(logging.INFO)

    app_log = logging.getLogger('root')
    app_log.setLevel(logging.INFO)
    app_log.addHandler(my_handler)

    session = gps.gps("localhost", "2947")
    session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

    while True:

        try:
            report = session.next()
            if hasattr(report, 'gdop'):
                satcount = 0
                for s in report['satellites']:
                    if s['used'] == True:
                        satcount+=1
                data = "{}".format(str(satcount))
                app_log.info(data)

        except KeyError:
            pass
        except KeyboardInterrupt:
            quit()
        except StopIteration:
            session = None

if __name__ == "__main__":
    main()
1

There are 1 best solutions below

0
On

This ended up being a result of failing hardware. Thank you for your help, @RobertB.