thinlog.formatters package#

Log formatters for JSON, plain-text, and Telegram output.

class thinlog.formatters.JsonFormatter(show_locals: bool = False, **kwargs: Any)[source]#

Bases: Formatter

Serialize the full log record as a JSON string.

Exception tracebacks are converted to structured dicts using structlog.tracebacks.ExceptionDictTransformer, and stack-info frames are extracted via parse_stack_info().

Parameters:

show_locals – If True, local variables are included in exception tracebacks.

format(record: LogRecord) str[source]#

Return the record as a JSON string.

formatException(ei: tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]) str[source]#

Format exception info as a JSON string, falling back to the standard formatter.

classmethod format_exception(ei: tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]) Any[source]#

Transform exception info into a structured dict via structlog.

classmethod format_record(record: LogRecord) dict[str, Any][source]#

Convert a LogRecord to a plain dict.

Handles exc_info (via structlog transformer) and stack_info (via parse_stack_info()).

transformer: ClassVar[ExceptionDictTransformer]#
class thinlog.formatters.MsgFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]#

Bases: Formatter

Return just the formatted message string, discarding all other record fields.

format(record: LogRecord) str[source]#

Return record.getMessage().

class thinlog.formatters.TelegramFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]#

Bases: Formatter

Format log records as HTML <code> blocks for the Telegram Bot API.

Messages that fit within MAX_MESSAGE_LEN (4096 chars) are sent as regular messages. Longer messages are split: a truncated caption (up to MAX_CAPTION_LEN chars) is sent alongside the full text as a document attachment.

MAX_CAPTION_LEN = 1024#
MAX_MESSAGE_LEN = 4096#
format(record: LogRecord) str[source]#

Format the record message, pretty-printing JSON if possible.

format_advanced(record: LogRecord) tuple[str | None, str][source]#

Return a (caption, text) tuple for Telegram delivery.

  • If the message fits in a single Telegram message, caption is None and text contains the full HTML-wrapped content.

  • If the message is too long, caption is a truncated HTML preview and text is the raw full-length message (sent as a document).

parse_mode = 'HTML'#

Submodules#