Log toolkit
Project description
Thinlog
A lightweight, fully-typed Python logging toolkit — a thin wrapper on the standard logging library with extra wheels.
Table of Contents
Features
- Wildcard logger configuration — define a
"*"logger and have it applied to every registered logger. - Keyword-friendly logging — pass keyword arguments directly; they become
extrafields. - Advanced filters — whitelist, blocklist, and conditional attribute assignment via TOML config.
- Structured JSON output — rich exception context powered by structlog.
- Remote logging — send logs to an HTTP endpoint or Telegram chat out of the box.
- Fully typed — strict MyPy compliance with a
py.typedPEP 561 marker.
Installation
pip install thinlog
Quick Start
config.toml:
[logging]
root = {level = "DEBUG", handlers = ["queue"]}
[logging.formatters]
json = {"()" = "thinlog.formatters.json.JsonFormatter", show_locals = false}
msg = {"()" = "thinlog.formatters.msg.MsgFormatter"}
[logging.handlers]
stream = {class = "logging.StreamHandler", level = "DEBUG", stream = "ext://sys.stderr", formatter = "msg"}
queue = {class = "logging.handlers.QueueHandler", handlers = ["stream"], formatter = "json", respect_handler_level = true}
app.py:
import tomllib
from pathlib import Path
from thinlog import configure_logging
config = tomllib.loads(Path("config.toml").read_text())
logger = configure_logging("myapp", config["logging"])
logger.info("app_started")
logger.warning("processing_payment_failed", user_id=42, ip="10.0.0.1")
From any other component or file if you need a specific logger you can simply do:
from thinlog import get_logger
logger = get_logger("my_other_specific_logger", dict(more="data", we="can pass"))
# Rest of the file...
A recommendation: Context(ctx) is good, use context everywhere, your app can have its own context, every request can have its own ctx so you can easily access your resources(e.g myapp logger) from ctx.
Concepts
Structured Log Keys
Thinlog treats the first argument of a log call as a machine-readable key, not a human-readable sentence. Use lowercase, underscore-separated identifiers that describe the event:
# Preferred — a stable, searchable key
logger.error("payment_gateway_timeout", order_id=512, gateway="stripe")
# Avoid — a free-form sentence that is hard to filter or aggregate
logger.error("The payment gateway timed out while processing order 512")
Structured keys are easy to match with filters, trivial to GROUP BY in a log aggregation system, and never require fragile regular expressions to parse. Pair them with keyword arguments for all variable data and you get logs that are both compact and rich in context.
Since this library is optimized so that each component have its own logger, the key can be short and optimized.
from thinlog import get_logger
logger = get_logger("my_other_specific_logger", dict(more="data", we="can pass"))
logger.error("request_failed", id=2)
# If there are multiple loggers with the key set to `request_failed`, it won't conflict.
# since it belongs to `my_other_specific_logger`,
# we can easily find the cause and easily filterable in logging stacks.
configure_logging
configure_logging is a plain function that returns a ready-to-use logger. It registers an atexit handler that automatically stops QueueHandler listeners and flushes handlers on interpreter exit.
Wildcard Loggers
Define a "*" logger in your config and it will be applied to all registered loggers. Use "merge": true on a specific logger to extend rather than replace the wildcard config.
[logging.loggers]
"*" = {level = "INFO", handlers = ["queue"]}
[logging.loggers.trace_log]
merge = true
handlers = ["trace_handler"]
Filters
- WhitelistFilter — allow records matching name, message, or attribute patterns.
- BlocklistFilter — block matching records (inverse of whitelist).
- AssignerFilter — conditionally assign attributes to matching records without blocking any.
[logging.filters]
skip_noisy = {"()" = "thinlog.filters.blocklist.BlocklistFilter", by_name = ["urllib3", "httpx"]}
Handlers
- JsonHTTPHandler — send JSON logs via HTTP POST with per-record URL/header overrides.
- TelegramHandler — send logs to Telegram; auto-splits long messages into document attachments.
- CtxPrintHandler — print record context as JSON to stdout for development.
Formatters
- JsonFormatter — full record as JSON with structured exception tracebacks.
- MsgFormatter — plain message string only.
- TelegramFormatter — HTML-formatted output with length-aware splitting.
Documentation
Full documentation is available at minfrastructure.github.io/thinlog.
This module is fully typed and compatible with MyPy out of the box. Please open an issue for any suggestions or bugs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file thinlog-26.2.3.tar.gz.
File metadata
- Download URL: thinlog-26.2.3.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18b80857661b9e5127fa43ac24573314ec51d59d4a46151e1bb40bb5857e1880
|
|
| MD5 |
59894f87f8e6b8cfe3ae8390c18a7428
|
|
| BLAKE2b-256 |
e8c7bdcdb9a096263ce044c77977fe2c98704e405789b34aa460dd940c76b82a
|
Provenance
The following attestation bundles were made for thinlog-26.2.3.tar.gz:
Publisher:
publish.yml on minfrastructure/thinlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
thinlog-26.2.3.tar.gz -
Subject digest:
18b80857661b9e5127fa43ac24573314ec51d59d4a46151e1bb40bb5857e1880 - Sigstore transparency entry: 1003418920
- Sigstore integration time:
-
Permalink:
minfrastructure/thinlog@3b79ca2069424845b073398e5103e3b0e762e1e4 -
Branch / Tag:
refs/tags/26.02.3 - Owner: https://github.com/minfrastructure
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b79ca2069424845b073398e5103e3b0e762e1e4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file thinlog-26.2.3-py3-none-any.whl.
File metadata
- Download URL: thinlog-26.2.3-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50bdbd4d3c5b8f42ff2ffecbdae40a3b4e55d4deebc425d4ff1ecd97f39124dc
|
|
| MD5 |
4dd36df849201257f497fcecdf81f2ee
|
|
| BLAKE2b-256 |
c78f979cb15a7635bd90be35640025e7430a838306e3c317ca278945a9accf24
|
Provenance
The following attestation bundles were made for thinlog-26.2.3-py3-none-any.whl:
Publisher:
publish.yml on minfrastructure/thinlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
thinlog-26.2.3-py3-none-any.whl -
Subject digest:
50bdbd4d3c5b8f42ff2ffecbdae40a3b4e55d4deebc425d4ff1ecd97f39124dc - Sigstore transparency entry: 1003418934
- Sigstore integration time:
-
Permalink:
minfrastructure/thinlog@3b79ca2069424845b073398e5103e3b0e762e1e4 -
Branch / Tag:
refs/tags/26.02.3 - Owner: https://github.com/minfrastructure
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b79ca2069424845b073398e5103e3b0e762e1e4 -
Trigger Event:
push
-
Statement type: