thinlog.handlers package#
Log handlers for stdout, HTTP, and Telegram delivery.
- class thinlog.handlers.CtxPrintHandler(level=0)[source]#
Bases:
HandlerPrint the
contextattribute of a log record as JSON to stdout.If the record has no
contextattribute the handler silently does nothing. Useful for debugging context filters during development.
- class thinlog.handlers.JsonHTTPHandler(settings: JsonHttpHandlerSettings | dict[str, Any])[source]#
Bases:
HandlerSend 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_httpdict on the log record.- Parameters:
settings – A
JsonHttpHandlerSettingsor a dict of its fields.
- property client: Client#
Lazily initialised
httpx.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.
- class thinlog.handlers.TelegramHandler(settings: TelegramHandlerSettings | dict[str, Any])[source]#
Bases:
HandlerSend log records to a Telegram chat via the Bot API.
Short messages are sent as regular Telegram messages. Messages exceeding
MAX_MESSAGE_LENare uploaded as document attachments with a truncated caption.Per-record overrides (chat_id, token, topic_id, disable) can be provided via a
handlers_context.telegramdict on the log record.- Parameters:
settings – A
TelegramHandlerSettingsor a dict of its fields.
- API_ENDPOINT = 'https://api.telegram.org'#
- property client: Client#
Lazily initialised
httpx.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 viasendDocument. Supports per-record overrides viarecord.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.
Submodules#
- thinlog.handlers.ctx_print module
- thinlog.handlers.json_http module
- thinlog.handlers.telegram module
TelegramHandlerTelegramHandlerSettingsTelegramHandlerSettings.chat_idTelegramHandlerSettings.disable_notificationTelegramHandlerSettings.disable_web_page_previewTelegramHandlerSettings.global_instance()TelegramHandlerSettings.levelTelegramHandlerSettings.proxyTelegramHandlerSettings.timeoutTelegramHandlerSettings.tokenTelegramHandlerSettings.topic_id