这对python日志库来说是一件简单的事吗。假设我有一个像这样的日志输出:
[2016-02-19 10:47:21,128] [ INFO] --- Setting up window... (Calipso.py:690)
[2016-02-19 10:47:21,128] [ INFO] --- Placed toolswindow at: 1x1+1642+373... (Calipso.py:184)
[2016-02-19 10:47:21,128] [ INFO] --- Setting up menu... (Calipso.py:692)
[2016-02-19 10:47:21,144] [ INFO] --- Setting up main screen... (Calipso.py:694)
[2016-02-19 10:47:21,144] [ INFO] --- Creating upper program GUI... (Calipso.py:192)
[2016-02-19 10:47:21,144] [ INFO] --- Setting up toolbar... (toolswindow.py:86)
[2016-02-19 10:47:21,144] [ INFO] --- Creating toolbar buttons... (toolswindow.py:154)
[2016-02-19 10:47:21,144] [ INFO] --- Setting initial plot... (Calipso.py:210)
[2016-02-19 10:47:21,144] [ WARNING] --- set_plot called for BASE_PLOT... (manager.py:249)
[2016-02-19 10:47:24,779] [ INFO] --- Importing HDF file... (Calipso.py:237)
[2016-02-19 10:47:30,861] [ INFO] --- Grabbing range and safe checking... (toolswindow.py:335)
[2016-02-19 10:47:30,861] [ INFO] --- Calling plot... (toolswindow.py:339)
[2016-02-19 10:47:30,861] [ ERROR] --- Needs to be reimplemented... (Calipso.py:370)
[2016-02-19 10:47:31,549] [ ERROR] --- IOError, no file exists... (Calipso.py:385)
现在显然发生了一些不好的事情,但如果由于某种原因程序仍然有效(可能该文件是后端文件而用户不知道它是否已加载),他可能会在下次覆盖日志文件他在不知不觉中删除了该错误的任何日志。
如果日志中出现error
,是否可以将日志另存为其他文件名?也许我可以创建某种python析构函数,以便在程序即将关闭时,我可以打开文件并查找任何错误或其他内容?
P.S。如果我的实际记录器配置相关,我可以发布。
答案 0 :(得分:0)
您可以使用MemoryHandler
缓冲内存中的记录,并在出现错误(或更高)时输出:
MemoryHandler类,位于logging.handlers模块中, 支持在内存中缓冲日志记录,定期刷新 他们到目标处理程序。只要缓冲区已满,就会发生刷新, 或者当发现某种严重程度或更高的事件时。