[Question]How can I show "pretty logging with colors" in the GUI?
Loguru's color logging is very pretty. Can I use it in the GUI to display colored info? I tried to "redirect" the stdout to a Text control and it (of course) does not display colors. What can I do? (sorry this is not an issue, and sorry for my bad English)
import tkinter as tk import sys from loguru import logger
class ExampleApp(tk.Tk): def init(self): tk.Tk.init(self) self.text = tk.Text(self) self.text.pack()
# stdout redirect sys.stdout = TextRedirector(self.text, "stdout") # add an logger (without remove ,for compare) logger.add(sys.stdout, level="TRACE") ##########TEST############################### print ("print to stdout") # print logger.error("log to stdout ") # loguru #############################################class TextRedirector(object): # thanks to Bryan Oakley's answer at https://stackoverflow.com/a/12352237/11807952 def init(self, widget, tag="stdout"): self.widget = widget self.tag = tag
def write(self, str): self.widget.configure(state="normal") self.widget.insert("end", str, (self.tag,)) self.widget.configure(state="disabled")app = ExampleApp() app.mainloop()
Hi @KongNan.
Is it possible to configure() your widget and colorize it? You'll probably need to add a handler in charge of creating and colorizing the text.
def add_log_to_gui(self, message):
color = {"INFO": "white", "DEBUG": "grey", "ERROR": "red", "WARNING": "yellow"}
self.widget.insert("end", message)
self.widget.configure(color=color.get(message["level"].name, "white"))
logger.add(add_log_to_gui)
Please, re-open the issue if my answer did not solve your issue.