mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 06:46:14 +00:00
90a082a0ac
## Summary
New `enable_logging=True` parameter on `responder.API()` that provides
structured, request-scoped logging using stdlib `logging` and
`contextvars`.
### What it does
- **`api.log`** — always available on every API instance. Works as a
plain logger by default; gains per-request context enrichment with
`enable_logging=True`
- **Per-request context** — every log message automatically includes
request ID, HTTP method, path, and client IP
- **Access logging** — logs every request with timing: `GET /path → 200
(1.2ms)`
- **Request ID** — generates or forwards `X-Request-ID` headers
(supersedes `request_id=True` when both are set)
- **stdlib logging** — works with any existing handler, formatter, or
log aggregator
### Usage
```python
# api.log always works — no setup required
api = responder.API()
api.log.info("starting up") # plain logger, no context
# With enable_logging=True, log messages get request context automatically
api = responder.API(enable_logging=True)
@api.route("/")
def index(req, resp):
api.log.info("handling request")
# => 2026-03-24 12:00:00 [INFO] responder.app — handling request [GET /] [req:abc123] [client:127.0.0.1]
```
For additional loggers in helper modules:
```python
from responder.ext.logging import get_logger
logger = get_logger("myapp.db")
```
### Architecture
- `responder/ext/logging.py` — self-contained module with:
- `LoggingMiddleware` — pure ASGI middleware that sets contextvars and
logs access
- `RequestContextFilter` — logging filter that injects context into
records
- `RequestContext` — read-only access to current request metadata
- `get_logger()` / `setup_logging()` — convenience functions
- `api.log` — always a valid logger; context-aware when
`enable_logging=True`, plain stdlib logger otherwise
- Wired into `API.__init__` via the `enable_logging` parameter
### Files
- `responder/ext/logging.py` — new module
- `responder/api.py` — added `enable_logging` parameter and `api.log`
- `tests/test_logging.py` — 9 tests
- `docs/source/tour.rst` — new Structured Logging section
- `docs/source/index.rst` — added to feature list
## Test plan
- [x] 9 logging tests pass
- [x] Full suite: 208 passed
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
136 lines
4.1 KiB
ReStructuredText
136 lines
4.1 KiB
ReStructuredText
Responder
|
|
=========
|
|
|
|
A familiar HTTP Service Framework for Python.
|
|
|
|
.. code:: python
|
|
|
|
import responder
|
|
|
|
api = responder.API()
|
|
|
|
@api.route("/{greeting}")
|
|
async def greet_world(req, resp, *, greeting):
|
|
resp.text = f"{greeting}, world!"
|
|
|
|
if __name__ == '__main__':
|
|
api.run()
|
|
|
|
Powered by `Starlette`_, `uvicorn`_, and good intentions. The ``async`` is optional.
|
|
|
|
|
|
The Idea
|
|
--------
|
|
|
|
If you've ever used `Flask`_, the routing will look familiar. If you've
|
|
used `Falcon`_, the request/response pattern will click immediately. And
|
|
if you've used `Requests`_ — well, you'll feel right at home.
|
|
|
|
Responder takes these ideas and brings them together. Every view receives
|
|
a request and a response. You read from one and write to the other. No
|
|
return values, no special response classes, no boilerplate.
|
|
|
|
- ``resp.text`` sends text. ``resp.html`` sends HTML. ``resp.media`` sends JSON.
|
|
- ``resp.file("path")`` serves a file. ``resp.content`` sends raw bytes.
|
|
- ``req.headers`` is case-insensitive. ``req.params`` holds query parameters.
|
|
- ``resp.status_code``, ``req.method``, ``req.url`` — the familiar ones.
|
|
|
|
Set ``resp.media`` to a dict and the right thing happens. If the client
|
|
asks for YAML, it gets YAML. Content negotiation is automatic.
|
|
|
|
Responder and `FastAPI`_ are siblings — both built on Starlette, both
|
|
born around the same time, both part of the push that made ASGI the
|
|
future of Python web services. FastAPI went deep on type annotations
|
|
and automatic validation. Responder went for simplicity and a mutable
|
|
request/response pattern. Both projects are better for the other
|
|
existing. Use whichever feels right.
|
|
|
|
This is a passion project. It exists because building a web framework
|
|
from scratch is one of the best ways to understand how the web works.
|
|
It's a great fit for personal projects, prototyping, teaching, research,
|
|
and anyone who values a clean API over a sprawling ecosystem. If you
|
|
need battle-tested infrastructure at scale, FastAPI and Django will
|
|
serve you well. If you want something small, expressive, and fun to
|
|
work with — welcome.
|
|
|
|
|
|
What You Get
|
|
------------
|
|
|
|
One ``pip install``, batteries included:
|
|
|
|
- Pydantic request validation and response serialization.
|
|
- Mount Flask, Django, or any WSGI/ASGI app at a subroute.
|
|
- Gzip compression, HSTS, CORS, and trusted host validation.
|
|
- Before-request and after-request hooks for auth and logging.
|
|
- A test client for fast, in-process testing with pytest.
|
|
- Route parameters with f-string syntax and type convertors.
|
|
- Lifespan context managers for startup and shutdown logic.
|
|
- Custom exception handlers for clean error responses.
|
|
- `GraphQL`_ with Graphene and a built-in GraphiQL IDE.
|
|
- Server-Sent Events for real-time streaming.
|
|
- File serving with automatic content-type detection.
|
|
- Sync and async views — ``async`` is always optional.
|
|
- Class-based views with ``on_get``, ``on_post``, ``on_request``.
|
|
- Built-in rate limiting with ``X-RateLimit`` headers.
|
|
- Structured logging with per-request context.
|
|
- Content negotiation: JSON, YAML, and MessagePack.
|
|
- A pleasant API with a single import statement.
|
|
- OpenAPI schema generation with Swagger UI.
|
|
- A production `uvicorn`_ server, ready to deploy.
|
|
- Route groups for API versioning.
|
|
- Signed cookie-based sessions.
|
|
- Background tasks in a thread pool.
|
|
- WebSocket support.
|
|
|
|
|
|
Installation
|
|
------------
|
|
|
|
.. code-block:: shell
|
|
|
|
$ uv pip install responder
|
|
|
|
Python 3.10 and above. That's it.
|
|
|
|
|
|
.. toctree::
|
|
:maxdepth: 2
|
|
:caption: User Guide
|
|
|
|
quickstart
|
|
tour
|
|
deployment
|
|
testing
|
|
api
|
|
cli
|
|
|
|
.. toctree::
|
|
:maxdepth: 2
|
|
:caption: Tutorials
|
|
|
|
tutorial-rest
|
|
tutorial-sqlalchemy
|
|
tutorial-auth
|
|
tutorial-websockets
|
|
tutorial-middleware
|
|
tutorial-flask
|
|
guide-config
|
|
|
|
.. toctree::
|
|
:maxdepth: 1
|
|
:caption: Project
|
|
|
|
changes
|
|
Sandbox <sandbox>
|
|
backlog
|
|
|
|
|
|
.. _Starlette: https://www.starlette.io/
|
|
.. _uvicorn: https://www.uvicorn.org/
|
|
.. _Flask: https://flask.palletsprojects.com/
|
|
.. _Falcon: https://falconframework.org/
|
|
.. _FastAPI: https://fastapi.tiangolo.com/
|
|
.. _GraphQL: https://graphql.org/
|
|
.. _Requests: https://requests.readthedocs.io/
|