[英]Python logging to QTextEdit
我想升級我的日志部分以適應日志記錄模塊。
我的應用程序已經相當先進並且使用 PySide 作為 GUI。 我想設置處理程序來生成不同的日志文件,還有一個寫入類似QTextEdit
控制台的小部件......
現在writeLog
function 寫入主日志文件(包含執行期間生成的所有日志)和QTextEdit
,此外,我為我的應用程序的某些特定部分寫入單獨的文件。
我怎樣才能做到這一點? (越簡單越好)。 我需要繼承 Handler class 嗎? (目前在 Python 中會大大高於我的水平,但如果指導得當,我猜為什么不呢)還是我只是錯過了文檔中的某些內容?
您可以使用自定義記錄器,而不是writeLog
函數。 這很容易。 例:
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)
現在logging.info("nice")
會將日志保存到GUI小部件。
要使用 QTextEdit 這必須改變
class GuiLogger(logging.Handler):
def emit(self, record):
self.edit.append_line(self.format(record))
和
class GuiLogger(logging.Handler):
def emit(self, record):
self.edit.textCursor().insertText(self.format(record))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.