Error Tracking

Capture unhandled crashes automatically or report handled exceptions manually.

TraceKit captures errors in two ways: automatically via Python exception hooks, and manually when you catch an exception yourself.

What it does

Calling tracekit.init() installs chained hooks for sys.excepthook (main thread) and threading.excepthook (worker threads). You can also attach an asyncio hook or call capture_exception() inside any except block.

Minimal example

errors.py
import threadingimport tracekit
tracekit.init(output="console", environment="production")
def parse_port(raw: str) -> int:    try:        return int(raw)    except ValueError as err:        tracekit.capture_exception(err)        return 8080
# Handled errorport = parse_port("not-a-number")print("Using fallback port:", port)
# Unhandled error in a worker threaddef broken_worker() -> None:    items = [1, 2]    _ = items[5]  # IndexError
t = threading.Thread(target=broken_worker)t.start()t.join()tracekit.flush()

Expected output

output
Using fallback port: 8080[TraceKit 2026-11-28 09:56:10] ERROR   ValueError: invalid literal for int() with base 10: 'not-a-number'  errors.py:8 in parse_portException in thread Thread-1 (broken_worker):Traceback (most recent call last):  ...IndexError: list index out of range[TraceKit 2026-11-28 09:56:10] ERROR   IndexError: list index out of range  errors.py:20 in broken_worker

Manual capture options

  • Inside an except block, call tracekit.capture_exception() with no arguments to capture the active exception from sys.exc_info().
  • Or pass an exception object explicitly: tracekit.capture_exception(err).
  • For non-exception alerts, use tracekit.capture_message("Cache miss spike", level="warning").

Common mistakes

  • Calling capture_exception() outside an except block with no argument: When there is no active exception, capture_exception() is a silent no-op.
  • Forgetting flush() in CLI scripts: The worker thread is a daemon thread; call tracekit.flush() before exiting so queued errors finish writing.