Python记录器将null(^ @)控制字符插入到文件中

时间:2017-09-08 17:50:36

标签: python logging gpsd

我正在从gpsd读取一些数据并使用Python的日志记录模块编写它。我相当只有一个进程写入此文件,尽管我在此记录器运行时使用tail读取它。每隔一段时间我就会看到一些看起来像下图的日志条目。我希望有人可以解释导致(可能是Python)将空控制字符插入我的日志文件的原因。

enter image description here

我使用的代码是:

"""
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 个答案:

答案 0 :(得分:0)

这最终导致硬件故障。谢谢你的帮助@RobertB。