stlog-fastapi-middlewares
FastAPI/starlette middlewares to improve logging with stlog library.
Installation
pip install stlog-fastapi-middlewares
(or equivalent command for your favorite package manager)
Usage (with FastAPI)
import stlog
from fastapi import FastAPI
from fastapi.concurrency import asynccontextmanager
from stlog_fastapi_middlewares import (
AccessLogMiddleware,
CustomExceptionHandlingMiddleware,
LogContextMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
stlog.setup()
yield
logger = stlog.getLogger(__name__)
app = FastAPI(lifespan=lifespan)
app.add_middleware(CustomExceptionHandlingMiddleware, logger=logger)
app.add_middleware(AccessLogMiddleware, logger=logger)
app.add_middleware(LogContextMiddleware, logger=logger)
@app.get("/")
async def root():
return {"message": "Hello World"}
Reference
We provide 3 independent middlewares.
CustomExceptionHandlingMiddleware
This middleware logs exceptions to structured logs.
No extra option is available.
Example:
import stlog
from fastapi import FastAPI
from fastapi.concurrency import asynccontextmanager
from stlog_fastapi_middlewares import (
CustomExceptionHandlingMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
stlog.setup()
yield
logger = stlog.getLogger(__name__)
app = FastAPI(lifespan=lifespan)
app.add_middleware(CustomExceptionHandlingMiddleware, logger=logger)
@app.get("/exception")
async def exception():
raise Exception("test")
AccessLogMiddleware
This middleware logs access to the application (to get access logs in JSON format for example).
You can optionally provide an ignore_hook option to ignore (for access log only!) some URLs.
Example:
import stlog
from fastapi import FastAPI
from fastapi.concurrency import asynccontextmanager
from stlog_fastapi_middlewares import (
AccessLogMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
stlog.setup()
yield
logger = stlog.getLogger(__name__)
app = FastAPI(lifespan=lifespan)
app.add_middleware(AccessLogMiddleware, logger=logger)
@app.get("/")
async def root():
return {"message": "Hello World"}
LogContextMiddleware
This middleware adds some request context to structured logs (think of automatic request id, process id...).
There are multiple options to customize the middleware. See the corresponding code for reference.
Example:
import os
import stlog
from fastapi import FastAPI
from fastapi.concurrency import asynccontextmanager
from stlog_fastapi_middlewares import (
LogContextMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
stlog.setup()
yield
logger = stlog.getLogger(__name__)
app = FastAPI(lifespan=lifespan)
app.add_middleware(
LogContextMiddleware,
logger=logger,
add_pid=True,
add_request_id=True,
envs_to_kvs={"FOO": "foo", "BAR": "bar"},
headers_to_kvs={"X-Test-Id": "test_id"},
)
# Let's add some environment variables to test the middleware
os.environ["FOO"] = "foo2"
os.environ["BAR"] = "bar2"
@app.get("/")
async def root():
logger.info("hello world")
# => this call will output a log with the following extra keys:
# - foo
# - bar
# - pid
# - request_id
# - test_id (if the call is made with a X-Test-Id header)
return {"message": "Hello World"}
Metadata
Release files for stlog-fastapi-middlewares 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| stlog_fastapi_middlewares-0.2.1.tar.gz | 6.3 kB | Without provenance | Details for stlog_fastapi_middlewares-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stlog_fastapi_middlewares-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for stlog_fastapi_middlewares-0.2.1-py3-none-any.whl |
Total release size: 13.3 kB
Release files / stlog_fastapi_middlewares-0.2.1.tar.gz
| Download URL | stlog_fastapi_middlewares-0.2.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3f6431cc708680d66c1dff3e1a2ef94eae045e08812e5a5501fb9e4d13e81bb8
|
|
BLAKE2b-256 checksum How to use checksums |
307df11c8d0b80564a848f1067a1f9291edfd69a32f55b10e510089af221f49a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.8
|
Release files / stlog_fastapi_middlewares-0.2.1-py3-none-any.whl
| Download URL | stlog_fastapi_middlewares-0.2.1-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d647951c3f35f2cad3b1f9831c0c19c473ced96e14ae14d7b01c9e3ca19b30c
|
|
BLAKE2b-256 checksum How to use checksums |
08362f51cbcfb9bb1c8821843cd04d585834f228d0735ca3359ef6075cc8ae77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.8
|
Security for stlog-fastapi-middlewares 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.