Home > database >  How to expand Python logging line with with message truncated with ellipse in the middle?
How to expand Python logging line with with message truncated with ellipse in the middle?

Time:10-23

I have log files generated from Python's logging module that truncates with ellipses in the middle of lines, at around 159 characters or 160 if you count newline.

At first, I thought it was VSCode doing the truncating. The file was loaded into Notepad and the lines were the same width. It's likely the lines were truncated with Python's logging module.

Settings for logging module:

import logging as log
from logging.handlers import RotatingFileHandler

log.basicConfig(
        handlers=[RotatingFileHandler('./logs/kucoin_bot.log', maxBytes=100000, backupCount=100, encoding='utf-8')],
        level=log.DEBUG,
        format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
        datefmt='%Y-%m-%dT%H:%M:%S')

Prettier extension is installed in VSCode. What are the options to expand this line to its full width to show all the details of the log?

[2022-10-22T01:47:04] DEBUG [websockets.client.read_frame:1152] < TEXT '{"type":"message","topic":"/spotMarket/level2De...estamp":1666428417709}}' [394 bytes]

The following setting in VSCode didn't make a difference:

"editor.stopRenderingLineAfter" : -1

Update:

I wanted to find out if Python's logging was reformating the string. The function in Python's websockets that generated the truncated line was located:

def write_frame_sync(self, fin: bool, opcode: int, data: bytes) -> None:
    frame = Frame(fin, Opcode(opcode), data)
    if self.debug:
        self.logger.debug("> %s", frame)
        print(frame) # <<<
        frame.write(
        self.transport.write,
        mask=self.is_client,
        extensions=self.extensions,
    )

A print() statement was used to print out the 'frame' (notated by <<<).

TEXT '{"type": "subscribe", "topic": "/spotMarket/lev...privateChannel": false}' [1063 bytes]

The result looked similarly truncated!

CodePudding user response:

It's likely the lines were truncated with Python's logging module.

That's not part of out-of-the-box logging functionality; if it's not an artifact of the editor, it's likely a Formatter was configured which has the line-shortening behaviour.

CodePudding user response:

Based on the updated I posted in the original post, I looked into the code of what the Frame object was. The file was found at:

C:\Users\Thomas\AppData\Local\Programs\Python\Python310\Lib\site-packages\websockets\frames.py

There is a function to return a string to make the frame object human readable:

def str(self) -> str: """ Return a human-readable representation of a frame.

"""

Toward the end of the function, the code to truncate the data was found:

if len(data) > 75:
    data = data[:48]   "..."   data[-24:] <---<<< here

metadata = ", ".join(filter(None, [coding, length, non_final]))

return f"{self.opcode.name} {data} [{metadata}]"

In summary, the frame object gets truncated when translated to a string and it's too large. Python's logging and VSCode weren't the ones doing the truncating.

  • Related