Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 214
Manu310 Avatar asked Nov 04 '25 15:11

Manu310


1 Answers

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.

like image 51
krrr Avatar answered Nov 06 '25 07:11

krrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!