I want to upgrade my logs parts to fit the logging module.
My application is already quite advanced and uses PySide for GUI. I would like to set handlers to generate different log files, but also one to write to a QTextEdit
console-like widget...
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.
How can I achieve this? (the simpler the better). Do I need to subclass 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?
You can use custom logger instead of your writeLog
function. 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.
To use QTextEdit this has to changed
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.