[英]Python logging to QTextEdit
I want to upgrade my logs parts to fit the logging module.我想升级我的日志部分以适应日志记录模块。
My application is already quite advanced and uses PySide for GUI.我的应用程序已经相当先进并且使用 PySide 作为 GUI。 I would like to set handlers to generate different log files, but also one to write to a QTextEdit
console-like widget...我想设置处理程序来生成不同的日志文件,还有一个写入类似QTextEdit
控制台的小部件......
For now a writeLog
function writes to a main log file (containing all logs generated during execution), and to the QTextEdit
, and in addition, I write to separate files for some specific parts of my application.现在writeLog
function 写入主日志文件(包含执行期间生成的所有日志)和QTextEdit
,此外,我为我的应用程序的某些特定部分写入单独的文件。
How can I achieve this?我怎样才能做到这一点? (the simpler the better). (越简单越好)。 Do I need to subclass Handler class?我需要继承 Handler class 吗? (would be quite above my level for now in Python, but if well guided, why not I guess) Or did I simply miss something in the doc? (目前在 Python 中会大大高于我的水平,但如果指导得当,我猜为什么不呢)还是我只是错过了文档中的某些内容?
You can use custom logger instead of your writeLog
function. 您可以使用自定义记录器,而不是writeLog
函数。 It's quite easy. 这很容易。 example: 例:
class GuiLogger(logging.Handler):
def emit(self, record):
self.edit.append_line(self.format(record)) # implementation of append_line omitted
h = GuiLogger()
h.edit = yourTextEditWidget # this should be done in __init__
logging.getLogger().addHandler(h)
and now logging.info("nice")
will save log to GUI widget. 现在logging.info("nice")
会将日志保存到GUI小部件。
To use QTextEdit this has to changed要使用 QTextEdit 这必须改变
class GuiLogger(logging.Handler):
def emit(self, record):
self.edit.append_line(self.format(record))
with和
class GuiLogger(logging.Handler):
def emit(self, record):
self.edit.textCursor().insertText(self.format(record))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.