I figure out how you can manage to that. First, we need to intercept the stdout process of Rich logger. We start with a class:
from collections import deque
class Logger():
_instance = None
def __init__(self):
self.messages = deque(["sa"])
self.size = 10
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)
return class_._instance
def write(self, message):
self.messages.extend(message.splitlines())
while len(self.messages) > self.size:
self.messages.popleft()
def flush(self):
pass
which is a Singleton class. We need to pass this class into console API as
from rich.console import Console
c = Console(file=Logger(), width=150)
with some width. Then, we create a logging handler
from rich.logging import RichHandler
r = RichHandler(console=c)
This will be our logging handler as
import logging
logging.basicConfig(
level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[r]
)
logger = logging.getLogger("rich")
Later on, we need to use our Logger class called somewhere you manage your layout. For me, it is inside a Dashboard class.
class Dashboard:
def __init__(self):
self.log_std = Logger()
def update(self, new_parameters):
self.layout["logs"].update(Panel(Text(
"\n".join(self.log_std.messages), justify="right"), padding=(1, 2),
title="[b red]Logs",
))
Each time I call update
method, it updates my layout.
My layout is more complex, self.layout["logs"]
where I show the logs.