Skip to main content

tg-logging-handler logo

tg-logging-handler

CI CodeQL codecov PyPI Python versions License: MIT

Production-grade Telegram destination for Python's standard logging module.

Attach one handler and your logs go to a Telegram chat — asynchronously, so a slow or down Bot API never blocks your application. Batching, retries with backoff, 429 handling, oversized-message splitting, bounded-queue back-pressure, and parse_mode escaping are all handled for you.

Install

pip install tg-logging-handler

Requires Python 3.10+. The only runtime dependency is httpx.

Quickstart

import logging
from tg_logging_handler import TelegramLoggingHandler

# Reads TG_TOKEN / TG_CHAT_ID from the environment.
logging.getLogger().addHandler(TelegramLoggingHandler())

logging.error("Something broke")  # delivered to your Telegram chat

TGLoggingHandler is a shorter alias for the same class.

Provide credentials explicitly instead of via the environment:

handler = TelegramLoggingHandler(token="123456:ABC-DEF", chat_id=-100123456789)
logging.getLogger().addHandler(handler)

Why this handler

  • Never blocks your app. emit() only snapshots the record and enqueues it; a single daemon thread does all network I/O. A dead Bot API can never stall a request handler.
  • Never crashes your app. Send failures are retried, then reported to stderr and counted — never raised into your code. The only exception you can catch is TelegramConfigError, and only at construction time.
  • No logging recursion. Internal diagnostics go to stderr, never back through logging.

Configuration

All constructor arguments after token / chat_id are keyword-only.

Argument Default Purpose
token None Bot token; falls back to TG_TOKEN.
chat_id None Target chat; falls back to TG_CHAT_ID.
level logging.WARNING Minimum level (passed to setLevel).
batch_size 1 Max records per message (>= 1). 1 = send each record immediately.
flush_interval 5.0 Max seconds a partial batch waits (>= 0).
max_retries 3 Retry budget for network/5xx errors (>= 0). 429s honor Retry-After without spending this budget (capped at 10 consecutive waits).
overflow "split" Oversized-message policy: "split", "truncate", or "drop".
parse_mode None None, "Markdown", "MarkdownV2", or "HTML". Formatter output is escaped for the chosen mode.
queue_maxsize 10_000 Bounded queue size. 0 = unbounded (memory risk).
queue_full_policy "drop_newest" When the queue is full: "block", "drop_newest", or "drop_oldest".
shutdown_timeout 5.0 Max seconds close() waits for the worker to drain.
validate True Make a synchronous getMe check at construction. Set False offline/in tests.
api_base_url "https://api.telegram.org" Override for self-hosted Bot API servers.

Environment variables

Variable Used when
TG_TOKEN token argument is omitted.
TG_CHAT_ID chat_id argument is omitted.

Batching

Batch records into fewer messages to stay well under Telegram's rate limits:

handler = TelegramLoggingHandler(
    level=logging.ERROR,
    batch_size=10,  # up to 10 records per message
    flush_interval=10.0,  # ...or flush a partial batch after 10s
)

A batch is sent when it fills (batch_size) or when flush_interval elapses, whichever comes first.

parse_mode / rich formatting

Set parse_mode and the handler escapes each formatted record so log content (tracebacks, arbitrary user data) can never break Telegram's parser:

handler = TelegramLoggingHandler(parse_mode="MarkdownV2")

Because the whole formatter output is escaped, a message like v1.2_final is delivered literally rather than 400-ing the request or rendering as accidental formatting.

dictConfig

LOGGING = {
    "version": 1,
    "handlers": {
        "telegram": {
            "()": "tg_logging_handler.TelegramLoggingHandler",
            "level": "ERROR",
            "batch_size": 5,
        },
    },
    "root": {"handlers": ["telegram"], "level": "WARNING"},
}

import logging.config

logging.config.dictConfig(LOGGING)

Inspecting delivery

handler.stats returns an immutable snapshot of cumulative counters:

s = handler.stats
print(s.queued, s.sent, s.batches_sent, s.retries, s.failed, s.dropped)

Shutdown

close() drains the queue (bounded by shutdown_timeout), stops the worker, and releases the HTTP client. It is idempotent and registered via atexit, so it runs automatically at interpreter exit. For deterministic tests, call it explicitly or use logging.shutdown().

Development

uv sync                          # create .venv + install package + dev deps
uv run pytest                    # run the test suite
uv run ruff check . && uv run mypy   # lint + type-check

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tg_logging_handler-0.1.2.tar.gz (362.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tg_logging_handler-0.1.2-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file tg_logging_handler-0.1.2.tar.gz.

File metadata

  • Download URL: tg_logging_handler-0.1.2.tar.gz
  • Upload date:
  • Size: 362.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for tg_logging_handler-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3965b48f8e1016e8f8b5249f4f39dd28f1d984b6b261dab6e6530d491f73365e
MD5 d7b4cc62298812ee124d951829640742
BLAKE2b-256 1f47cb72979288adadd38e4c9825f14cdb95dfa235dfb8b70be3004539767b06

See more details on using hashes here.

Provenance

The following attestation bundles were made for tg_logging_handler-0.1.2.tar.gz:

Publisher: publish-pypi.yml on 0xarchit/tg-logging-handler

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tg_logging_handler-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tg_logging_handler-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 984d17bbcac08280ba6456f764f7d263a5e9da9f1e3f42b8ac1de641525ba7f6
MD5 e0a7caec892a68647d82a558c94273af
BLAKE2b-256 ca9e545c5800ace334f8050faf06af354af4558d93e9fe36400c785b19228fc5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tg_logging_handler-0.1.2-py3-none-any.whl:

Publisher: publish-pypi.yml on 0xarchit/tg-logging-handler

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page