我使用 python 日志记录,并希望最新的日志条目位于日志文件的头部,而不是尾部。
我在 https://docs.python.org/2/howto/logging.html 中找不到任何内容这对于在日志文件的头部而不是尾部附加记录器记录似乎很有用。
这是我的记录器:(“Carl”是我的 GoPiGo3 机器人)
import logging
# create logger
logger = logging.getLogger('lifelog')
logger.setLevel(logging.INFO)
loghandler = logging.FileHandler('/home/pi/Carl/life.log')
logformatter = logging.Formatter('%(asctime)s|%(message)s',"%Y-%m-%d %H:%M")
loghandler.setFormatter(logformatter)
logger.addHandler(loghandler)
#logger.info('-------------')
我登录的是:
logger.info('<something to log>')
有 python 日志记录模块固有的解决方案吗? 我必须编写自己的处理程序吗?
最佳答案
答案:我不应该这样做。
因为:
Inserting something at the beginning of a file displaces everything that comes after it, so the bigger the file, the more an insert will cost. That would go counter to the efforts of the logging module to be sparing in its use of resources. – BoarGules Mar 28 at 13:56
使用“tac”查看日志文件是一个很好的解决方案。
关于Python记录到日志文件的头部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55399312/