Metadata-Version: 2.4
Name: shipbook
Version: 0.3.0
Summary: Shipbook logging SDK for Python servers — ships the standard library logging to Shipbook.
Project-URL: Homepage, https://shipbook.io
Project-URL: Documentation, https://www.shipbook.io/docs/python-log-integration
Project-URL: Source, https://github.com/ShipBook/shipbook-python
Author-email: Shipbook <support@shipbook.io>
License: MIT
License-File: LICENSE
Keywords: logging,logs,monitoring,observability,shipbook
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Logging
Classifier: Typing :: Typed
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: django; extra == 'dev'
Requires-Dist: flask; extra == 'dev'
Requires-Dist: httpx; extra == 'dev'
Requires-Dist: mcp>=2; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: starlette; extra == 'dev'
Provides-Extra: django
Requires-Dist: django; extra == 'django'
Provides-Extra: fastapi
Requires-Dist: starlette; extra == 'fastapi'
Provides-Extra: flask
Requires-Dist: flask; extra == 'flask'
Provides-Extra: mcp
Requires-Dist: mcp>=2; extra == 'mcp'
Description-Content-Type: text/markdown

# Shipbook Python SDK

Ship your Python server logs to [Shipbook](https://shipbook.io) — with **zero new logging API to learn.** The SDK plugs into Python's standard `logging` module, so the code you already write just works:

```python
import logging
import shipbook

shipbook.start(app_id="YOUR_APP_ID", app_key="YOUR_APP_KEY")

log = logging.getLogger(__name__)
log.info("user signed in")
log.error("payment failed", exc_info=True)
```

Every `logging` call in your process is captured, batched, and uploaded to Shipbook in the background. Source location (file, function, line), the logger name (as the tag), thread info, and exception tracebacks all come across automatically — because Python's `logging` already records them.

## Install

```bash
pip install shipbook
```

Zero required dependencies. Optional framework helpers:

```bash
pip install "shipbook[fastapi]"   # or [flask], [django], [mcp]
```

## How it works

`shipbook.start()` attaches a single `ShipbookHandler` to the root logger and returns at once; it logs in to Shipbook on a background thread, so an unreachable API never delays your app's startup. There is **no** separate Shipbook logging API — `ShipbookHandler` *is* the SDK. Logs are kept in memory and uploaded in batches by a background thread; nothing on your request path blocks on the network, and nothing is written to disk.

What ships is controlled by two independent gates:

1. **Standard `logging` levels** — records are only created if the logger's level allows them. To ship `DEBUG`/`INFO`, make sure the level permits it (or pass `capture_level=logging.DEBUG` to `start()`).
2. **Server-side config** — Shipbook's dashboard controls which tags/severities are actually uploaded, and can be changed without a redeploy. Until the first config arrives, logs are held and then filtered by it, so a filter set in the dashboard also applies to startup logs.

With the **Accumulate** config, logs below the warning level are held per request and uploaded only when that request logs a warning or error — as their context. Healthy requests cost nothing.

What your app prints stays as it is. Python prints warnings and errors by itself only while no handler is attached, so the SDK prints them the same way when it is the only handler; once your app configures logging, its own handlers decide.

`start()` takes your app's version from the nearest `pyproject.toml` (`[project]` or `[tool.poetry]`), from the working directory up. Pass `app_version="1.4.0"` to set it yourself.

To point the SDK at another Shipbook API (for example a staging environment), call `shipbook.set_connection_url(url)` before `start()`.

## Attaching to a specific logger

By default the handler is attached to the root logger. To scope it yourself:

```python
shipbook.start(app_id="...", app_key="...", attach_to_root=False)
logging.getLogger("myapp").addHandler(shipbook.get_handler())
```

## Web frameworks — read your server request by request

A `LogRecord` doesn't know which request it belongs to. The framework integrations bind each request to a Shipbook **session**, so logs carry the user, client IP, and trace id. Each request is also recorded with its method, route (`/items/{item_id}`), status and duration, and its logs are grouped under it in the Logbook. A failed request (5xx) is always recorded, even if it logged nothing, and an exception your code didn't handle is sent as an error of that request.

**FastAPI / Starlette**

```python
from shipbook.integrations.fastapi import ShipbookMiddleware
app.add_middleware(ShipbookMiddleware)
```

**Flask**

```python
from shipbook.integrations.flask import init_app
init_app(app)
```

**Django** (sync and async views) — add to `MIDDLEWARE`, after `SessionMiddleware` and `AuthenticationMiddleware`:

```python
"shipbook.integrations.django.ShipbookMiddleware"
```

Customize each request with extractors (FastAPI/Flask keyword arguments; on Django, subclass and override `get_user`, `get_session_id`, `get_trace_id`, `get_attributes`):

```python
app.add_middleware(
    ShipbookMiddleware,
    user_extractor=lambda request: shipbook.User(user_id=request.headers.get("x-user-id")),
    session_extractor=lambda request: request.cookies.get("login"),  # group requests into one session
    attributes_extractor=lambda request: {"tenant": request.headers.get("x-tenant")},
)
```

Or call `shipbook.register_user(...)` anywhere inside the request. By default every request is its own session; on Django a request joins its Django session when it has one.

On Django the signed-in user is attached without an extractor, once your view has read `request.user`: its id, username and email. The middleware never loads the user itself. Override `get_user` to attach something else, or to return `None` and attach nobody.

**Linked apps.** When a client app with a Shipbook SDK calls your API, it sends an `x-shipbook-session` header. The request joins that client session, so the Logbook shows the whole flow — the app's logs and your server's — in one view.

**JSON-RPC and MCP over HTTP.** A JSON-RPC call is labeled with what it asked for, e.g. `tools/call get-logs`, so calls to one endpoint stay apart.

## MCP servers

For an MCP server built with the `mcp` SDK (2.x), add the middleware and every call becomes a request, labeled with its operation, over any transport (stdio included):

```python
from mcp.server.mcpserver import MCPServer
from shipbook.integrations.mcp import ShipbookMiddleware

server = MCPServer("my-server", middleware=[ShipbookMiddleware()])
```

A tool that fails, raises or returns `isError`, is recorded as a failed request with its JSON-RPC error code.

## Background jobs

Wrap background work so its logs are grouped as a named session:

```python
with shipbook.run_in_context(job_name="nightly-billing"):
    logging.getLogger(__name__).info("charging customers")
```

Each run is its own session and counts as one request, like an HTTP request: it appears in the request counts and error rates, and the Logbook shows when it started, how long it took, and whether it raised.

Anything logged outside a request or job falls back to a per-process background session — nothing is lost.

To read other units of work (a queue message, a scheduled task) the way requests are read, run each one as a request:

```python
with shipbook.run_in_request(shipbook.RequestKind.Rpc, "process order"):
    process(order)
```

It's recorded with its duration and whether it finished or raised.

## Threads and executors

`contextvars` follow `await` and `asyncio` tasks automatically, but a manually
spawned thread or executor starts with a fresh context and would lose the
request's user/IP. Wrap the callable to carry the current context along:

```python
pool.submit(shipbook.preserve_context(do_work))
```

## Crashes and shutdown

An uncaught exception is captured and uploaded before Python prints it and exits, so the crash that killed your process reaches Shipbook. Exceptions in other threads are captured too, and exceptions in `asyncio` tasks arrive through `logging`.

An exception raised from another one names its cause in its reason: `could not load the order (caused by KeyError: 'missing')`.

Crashes are grouped in Loglytics by the innermost stack frame of your own code; frames from installed packages and the standard library are skipped. If your app is itself installed as a package (`pip install .`), name it so its frames count as yours:

```python
shipbook.start(app_id="...", app_key="...", in_app_include=["myapp"])
```

Queued logs are uploaded before the process ends: at interpreter exit and on `SIGTERM`/`SIGINT` (a container stop or a deploy), waiting at most 2 seconds. Your own signal handlers still run afterwards, and a default `SIGTERM` still terminates the process. To flush or stop explicitly:

```python
shipbook.flush()      # upload now and wait; flush(timeout=2) to bound it
shipbook.shutdown()
```

Forked workers (for example gunicorn with `--preload`) each upload their own logs.

## Troubleshooting

`shipbook.enable_inner_log(True)` prints the SDK's own diagnostics (login, uploads) to stderr.

## License

MIT
