Replies: 1 comment
-
This is something I ported from an old tkinter app. I doubt it is idiomatic Textual - I'd love to hear from Textual experts what the best way to do this is! Create a texthandler that keeps a reference to your textlog widget class TextHandler(logging.Handler):
"""Class for logging to a TextLog widget"""
def __init__(self, textlog):
# run the regular Handler __init__
logging.Handler.__init__(self)
# Store a reference to the Text it will log to
self.text = textlog
def emit(self, record):
msg = self.format(record)
style = "red"
# style your output depending on e.g record.levelno here
self.text.write(Text(msg, style)) Connect it up in the App's on_ready def on_ready():
...
text_log = self.query_one(TextLog)
logger = logging.getLogger("test")
logger.setLevel(logging.DEBUG)
th = TextHandler(text_log)
th.setLevel(logging.DEBUG)
logger.addHandler(th) Use it somewhere, output will appear in the TextLog log = logging.getLogger("test")
log.info("Something interesting happened") |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
While porting some CLI apps to Textual, the
TextLog
widget can be very useful for showing the logging output of the original CLI app.App.log
andtextual.log
only writes to the devtool, which is helpful for debugging but don't show user-facing info level logs. It would be cool if we can have an easy way to write log messages to aTextLog
widget transparently usingApp.log
ortextual.log
. This will also make migrating some CLI apps easier, as you don't need to pass a reference to theTextLog
widget to the legacy CLI business logic code issuing all the logs.Beta Was this translation helpful? Give feedback.
All reactions