Set a Minimum Width for Python Rich Logging (RichHandler)
Asked Answered
G

1

7

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)
Geode answered 12/6, 2022 at 15:2 Comment(2)
I think that gitlab is piping the output of locally runned commands and thus, RichHandler defaults to 80 character wide, which I think is pretty standard for a default. As for the original question, I cannot help any further.Terrell
@xcodz-dot That was my assumption as well. Fixing the output might be easier from the RichHandler side than the GitLab Runner console output side.Geode
M
7

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

E.g.

from rich.console import Console
richHandler = RichHandler(console=Console(width=255))
Misery answered 13/6, 2022 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.