0

sometimes I log big data and sometimes I don't. If the message is large, I want to crop it, but know how much I cropped it.

I want something like this:

big_data = [1, 2, 3, ... 100]

logger.info(f"Received data: {big_data})

Logging data:

11:20 INFO: Received data: [1, 2, 3, ... (another 100 symbols)

I can do it manually, but I would like to have a standard solution either at the logger setup level or at the info, erro etc method call level.

1 Answers1

0

Use a custom formatter:

FORMAT = "{time} {level} {message}" # example
MAX_LENGTH = 80 # including information such as time and level
def format_message(record: loguru.Record) -> string:
    formatted = FORMAT.format(record)
    if len(formatted) > MAX_LENGTH:
        formatted = f"{formatted[:MAX_LENGTH]}... (snipped {len(formatted) - MAX_LENGTH} characters)"
    return formatted
...
logged.add(..., format=format_message, ...)
pigrammer
  • 2,603
  • 1
  • 11
  • 24