Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Minimum Width for Python Rich Logging (RichHandler)

I am using RichHander to format log output inside Python. While this works great locally, when I run inside GitLab, it seems to default to using a terminal that is 80 characters wide. This makes the output fairly difficult to read and scan quickly. I would like to change this default width for the RichHandler, but I don't see a way to do it.

Is there a way to set a minimum console width for the Python RichHandler log handler?

# Pseudocode:
    
import logging
from rich.logging import RichHandler

def setup_logging():
  logger = logging.getLogger('myLogger')
  richFormatter = logging.Formatter('%(message)s')
  richHandler = RichHandler()
  # Something like: richHandler.setMinimumWidth(255)
  richHandler.setFormatter(richFormatter)
  logger.addHandler(richHandler)
like image 766
DiB Avatar asked Jun 18 '26 23:06

DiB


1 Answers

Add console=Console(width=255) to the handler constructor.

E.g.

from rich.console import Console
richHandler = RichHandler(console=Console(width=255))
like image 88
Will McGugan Avatar answered Jun 21 '26 12:06

Will McGugan



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!