Python SDK
Logs from Python
You need a log source token (WT15_LOGS_TOKEN, wt15ls_…): see custom log sources and the ingest API it wraps. Redaction is yours:
the SDK ships the text it is given.
Push lines
Section titled “Push lines”client.ingest.logs.push([{"message": "Payment failed", "log_level": "error", "stream_name": "billing"}])Lines go in chunks of at most 1,000 lines and 5,000,000 bytes, gzipped and keyed as metrics are. A message and a raw_line are cut at 65,536 UTF-8 bytes on a character boundary; a string that is not
well-formed text is made so (U+FFFD); a datetime logged_at becomes RFC 3339. A line without a non-blank message is refused with an InvalidPayloadError naming its index. Pushing a different batch under
a key you already used is refused 422 idempotency_key_reused.
The logging handler
Section titled “The logging handler”import loggingfrom watchtower15 import Clientfrom watchtower15.integrations.logging import WatchTower15Handler
handler = WatchTower15Handler(Client.from_environment(), stream="billing")logging.getLogger().addHandler(handler)logging.getLogger("billing").error("Payment failed", extra={"order": 42})WatchTower15Handler(client=None, *, stream=None, level=NOTSET, batch_size=100, flush_interval=1.0, max_queue=10_000, include_extra=False). client is a Client, a callable returning one (called at each
flush) or None: then Client.from_environment() is built at the first flush and kept, never at construction, so a malformed WT15_* value never raises from addHandler(). In a dict config:
LOGGING = {"version": 1, "handlers": {"wt15": {"class": "watchtower15.integrations.logging.WatchTower15Handler", "stream": "billing", "level": "INFO"}}, "root": {"handlers": ["wt15"], "level": "INFO"}}- Levels.
CRITICAL(50) and above →critical,ERROR→error,WARNING→warning,INFO→info, anything lower →debug. - The message is the record as your handler’s formatter formats it, so a traceback is part of it. With
include_extra=Truethe record’sextrafields follow it as JSON. stream_nameisstream, else the record’s logger name, kept only when it matches^[A-Za-z0-9._/@:+-]{1,255}$.- Never recursive. The SDK’s own
watchtower15logger is not shipped, so a failure to ship never ships itself. flush_interval=Nonestarts no thread: records wait fordrain(),flush()orclose().logging.shutdown()closes the handler, which sends what is queued.
structlog and loguru
Section titled “structlog and loguru”Both reach the handler through the standard library, and the handler gets one record per line.
# structlog: wrap for the stdlib formatter, and format on the handlerhandler.setFormatter(structlog.stdlib.ProcessorFormatter( processors=[structlog.stdlib.ProcessorFormatter.remove_processors_meta, structlog.processors.KeyValueRenderer(key_order=["event"])]))structlog.configure(processors=[structlog.stdlib.add_log_level, structlog.stdlib.ProcessorFormatter.wrap_for_formatter], logger_factory=structlog.stdlib.LoggerFactory(), wrapper_class=structlog.stdlib.BoundLogger)
# loguru: the handler is a sinkfrom loguru import loggerlogger.add(handler, format="{level} {message}", level="INFO")Batches, bounds and the breaker
Section titled “Batches, bounds and the breaker”Each handler has a LogShipper: a queue of 10,000 records (past it overflow is counted), batches of batch_size (100) or every second, each batch one push with a 3-second timeout and one retry. When
the API refuses, the shipper stops instead of hammering it: the volume stop (until Retry-After or resumes_at, at most an hour), a 60-second hold after a connection error, a 5xx or a 403, a throttle hold of
Retry-After, and a probe when one ends; the stop is per process and per token, shared by every handler. What was not sent is counted by reason (config, missing_token, invalid_payload, volume_limit, rate_limited, held,
connection, rejected_<status>, internal, overflow) and logged at most once a flush on the watchtower15 logger as WatchTower15 logs: lines not sent: <reason> <lines>; a standing reason is repeated at most
hourly. drain() never raises and returns a report (lines_sent, not_sent by reason, reports); flush() is drain() with the report dropped.