thinlog.handlers package#

Log handlers for stdout, HTTP, and Telegram delivery.

class thinlog.handlers.CtxPrintHandler(level=0)[source]#

Bases: Handler

Print the context attribute of a log record as JSON to stdout.

If the record has no context attribute the handler silently does nothing. Useful for debugging context filters during development.

emit(record: LogRecord) None[source]#

Print record.context as a JSON string.

class thinlog.handlers.JsonHTTPHandler(settings: JsonHttpHandlerSettings | dict[str, Any])[source]#

Bases: Handler

Send formatted log records as JSON via HTTP POST using httpx.

The HTTP client is lazily initialised on first use. Per-record overrides (URL, headers, disable) can be provided via a handlers_context.json_http dict on the log record.

Parameters:

settings – A JsonHttpHandlerSettings or a dict of its fields.

property client: Client#

Lazily initialised httpx.Client.

close() None[source]#

Close the underlying HTTP client.

emit(record: LogRecord) None[source]#

Format and POST the record as JSON.

Supports per-record overrides via record.handlers_context["json_http"]:

  • disable – skip this record entirely.

  • url – override the target URL.

  • headers – replace the default headers.

  • append_headers – merge additional headers.

json_request(url: str, data: str | dict[str, Any], **kwargs: Any) Any[source]#

POST data as JSON (parsing it first if it is a string).

request(url: str, **kwargs: Any) Any[source]#

Send an HTTP POST and return the parsed JSON response.

class thinlog.handlers.TelegramHandler(settings: TelegramHandlerSettings | dict[str, Any])[source]#

Bases: Handler

Send log records to a Telegram chat via the Bot API.

Short messages are sent as regular Telegram messages. Messages exceeding MAX_MESSAGE_LEN are uploaded as document attachments with a truncated caption.

Per-record overrides (chat_id, token, topic_id, disable) can be provided via a handlers_context.telegram dict on the log record.

Parameters:

settings – A TelegramHandlerSettings or a dict of its fields.

API_ENDPOINT = 'https://api.telegram.org'#
property client: Client#

Lazily initialised httpx.Client.

close() None[source]#

Close the underlying HTTP client.

emit(record: LogRecord) None[source]#

Format the record and send it to Telegram.

Short messages are sent via sendMessage; long messages are uploaded as a document via sendDocument. Supports per-record overrides via record.handlers_context["telegram"].

classmethod format_url(token: str, method: str) str[source]#

Build a Telegram Bot API URL for the given method.

request(method: str, bot_token: str | None = None, **kwargs: Any) Any[source]#

Call a Telegram Bot API method and return the JSON response.

send_document(text: str, document: BytesIO, **kwargs: Any) Any[source]#

Send a document with a caption via sendDocument.

send_message(text: str, **kwargs: Any) Any[source]#

Send a text message via sendMessage.

Submodules#