Skip to main content

Zero-config structured logging for Python — JSON output, auto-context, function tracing

Project description

snaplog

Zero-config structured logging for Python. One import, structured JSON output everywhere.

Zero dependencies. Auto-context. Function tracing. JSON by default.

Install

pip install snaplog

Quick Start

from snaplog import get_logger

log = get_logger()

log.info("Order processed", order_id="abc", total=49.99)
# → {"ts":"2026-03-24T21:08:00.123Z","level":"info","msg":"Order processed","order_id":"abc","total":49.99,"logger":"root"}

That's it. No config files. No handlers. No formatters. Just log.

Features

  • 🚀 Zero configget_logger() and go
  • 📝 Structured JSON — every log is a JSON object
  • 🎯 Auto-context — function name, file, line number captured automatically
  • 🔍 Function tracing@log.trace decorator adds timing
  • 🏷️ Request contextwith log.context(user_id=...) adds fields to all logs
  • 👁️ Human-readable mode — for development
  • 🔌 Stdlib integration — bridges to Python's logging module
  • 🧵 Thread-safe — context propagates correctly
  • 📦 Zero dependencies — pure Python

API

Basic Logging

from snaplog import get_logger

log = get_logger()
log.debug("Verbose details", step=1)
log.info("Something happened", user_id="u1")
log.warning("Careful!", retries=3)
log.error("Failed to connect", host="db1", error_code=503)
log.critical("System down", component="database")

Auto-Context

# Automatically includes function name, file, and line
def process_order(order_id):
    log.info("Processing", order_id=order_id)
    # → {..., "func":"process_order", "file":"orders.py", "line":42}

Function Tracing

@log.trace_fn
def fetch_user(user_id):
    return db.get(user_id)
# → {"level":"trace","msg":"enter: fetch_user","func":"fetch_user","args":{"user_id":"u1"}}
# → {"level":"trace","msg":"exit: fetch_user","func":"fetch_user","elapsed_ms":12.3,"result":"ok"}

Request Context

with log.context(request_id="r1", user_id="u42"):
    log.info("Processing request")  # includes request_id + user_id
    call_service()
    log.info("Done")  # still includes them
# Context automatically removed after `with` block

Human-Readable Output

from snaplog import get_logger, HumanFormatter

log = get_logger(formatter=HumanFormatter())
log.info("Hello", name="world")
# → 2026-03-24 21:08:00 [INFO] Hello  name=world

Min Level

log = get_logger(min_level="warning")  # debug, info suppressed

Stdlib Integration

import logging
from snaplog import SnaplogHandler

handler = SnaplogHandler()
logging.root.addHandler(handler)
logging.info("This goes through snaplog too")

Custom Output

# Write to a file
import sys
log = get_logger(output=open("app.log", "a"))

# Custom output function
def my_output(record_dict):
    requests.post("https://logs.example.com", json=record_dict)

log = get_logger(output=my_output)

Output Format

{
  "ts": "2026-03-24T21:08:00.123Z",
  "level": "info",
  "msg": "Order processed",
  "logger": "root",
  "func": "process_order",
  "file": "orders.py",
  "line": 42,
  "order_id": "abc",
  "total": 49.99
}

License

MIT

Project details


Download files

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

Source Distribution

snaplog-0.4.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

snaplog-0.4.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file snaplog-0.4.0.tar.gz.

File metadata

  • Download URL: snaplog-0.4.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for snaplog-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8886bf6df6a68b5cdac3f58f9fbb43be5b3716e6d8a14adde1da959c7dd2f7c6
MD5 fde37cb45b7fd904ff19b9b302fc9185
BLAKE2b-256 9f7c465a3dab1ae4e83a199cdd49cfc9300ee2485ca22b9b695c6437445e188b

See more details on using hashes here.

File details

Details for the file snaplog-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: snaplog-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for snaplog-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7b9ca4c4652125528c3b4e269328e144418b05038776908670bc787e89d113b
MD5 e58596c66187978a259896314281dbfd
BLAKE2b-256 01716a7ad4b064c9f2ab394e909bafe91422b8f4a3adb94a370c29a3d0ce2f07

See more details on using hashes here.

Supported by

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