[英]How to turn off logging buffer to get logs in real time with python command line tool?
[英]Python logging command does not work at real time
我在Tkinter(ScrolledText)中有一个日志记录小部件,它带有TextHandler
类,该类处理小部件中的日志和打印主题
class TextHandler(logging.Handler):
def __init__(self, text):
# run the regular Handler __init__
logging.Handler.__init__(self)
# Store a reference to the Text it will log to
self.text = text
def emit(self, record):
msg = self.format(record)
def append():
self.text.configure(state='normal')
self.text.insert(Tkinter.END, msg + '\n')
self.text.configure(state='disabled')
# Autoscroll to the bottom
self.text.yview(Tkinter.END)
self.text.after(0, append)
st = ScrolledText.ScrolledText(self, width=190, height=9, state='disabled')
st.configure(font='TkFixedFont')
st.place(x=0, y=539)
text_handler = TextHandler(st)
# Logging configuration
logging.basicConfig(filename='test.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Add the handler to logger
self.logger = logging.getLogger()
self.logger.addHandler(text_handler)
并调用logging.info(msg)
获取日志消息。 但有一个问题。 一切都很好,但是当此功能在进程之前调用时(例如某些在列表中起作用),我的日志将在此之后出现!
logging.info("message")
print "message"
for topic in news:
...
print
方法在这里工作正常,但存在logging
问题。 循环结束后我有日志消息。
那么...是什么问题呢?
您不应该使用after(0, ...)
我不确定这是否是唯一的问题,但这绝对是一个问题。 您正在饿死事件处理程序-“空闲”队列永远不会为空,因此它没有机会处理正常事件。 实际上,您已经创建了一个无限循环。
您应该给一个小的非零间隔,这将有助于解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.