logging and rendering a table #1799
-
firstly @willmcgugan , this is a fantastic useful library thank you. I have got some partial success doing the following but the styling disappears
I am sure there is some subtle trick I am missing to make it look like this My full experimental code is here import logging
from rich.logging import RichHandler
from rich.console import Console
from rich.table import Table
from rich.text import Text
logging.basicConfig(
level=logging.DEBUG,
handlers=[RichHandler(console = Console(width=150))] #width workaround for IPython default to 80 char
# force=True
)
log= logging.getLogger(__name__)
# %%
log.info("test") # This works great
log.error("[bold red blink]Server is shutting down![/]", extra={"markup": False})
log.error("[bold red blink]Server is shutting down![/]", extra={"markup": True})
# %%
print("-"*150)
# %%
def a_rich_table():
table = Table(title="Star Wars Movies")
table.add_column("Released", justify="right", style="cyan", no_wrap=True)
table.add_column("Title",style="magenta")
table.add_column("Box Office", justify="right", style="green")
table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
return table
# %%
#THIS WORKS Table column styles rendered perfectly
console = Console(width=150)
console.print(a_rich_table())
# %%
# THIS FAILS - prints the object id
log.debug(f"a table\n{a_rich_table()}")
# %%
#use capture to generate a string
console = Console(width=150)
with console.capture() as capture:
console.print(a_rich_table())
# %%
#THIS FAILS Table rendered as raw text with control chars
log.debug(f"a table\n{capture.get()}")
# %%
#THIS WORKS however looses the Table column styles
capture_text = Text.from_ansi(capture.get())
log.debug(f"a table\n{capture_text}")
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Rich may handle the rendering in RichHandler, but Python logging is not designed for anything other that simple strings. Keep in mind that logging can be configured to go anywhere: file, to syslog, to mail, etc. most of which don't support ansi control codes for color, and may break any formatting that Rich does. Which is why it is best to stick to simple string messages. If you are using logging purely to write to the console, consider using |
Beta Was this translation helpful? Give feedback.
Rich may handle the rendering in RichHandler, but Python logging is not designed for anything other that simple strings.
Keep in mind that logging can be configured to go anywhere: file, to syslog, to mail, etc. most of which don't support ansi control codes for color, and may break any formatting that Rich does. Which is why it is best to stick to simple string messages.
If you are using logging purely to write to the console, consider using
console.log
which can display Rich objects without any clever capturing of the output.