BlackSheep Request Logger
Efficient request logging middleware for BlackSheep web framework.
Features
- 🚀 High Performance: Minimal overhead with pre-computed configurations
- 🔒 Security: Automatic redaction of sensitive headers (Authorization, Cookie, etc.)
- ⚙️ Configurable: Customize log levels, headers, and output format
- 📝 Clean Logs: Structured request/response logging with duration tracking
- 🐍 Type Safe: Full type hints and mypy support
Installation
# With UV (recommended)
uv add blacksheep-request-logger
# With pip
pip install blacksheep-request-logger
Quick Start
from blacksheep import Application
from blacksheep_request_logger import RequestLoggingMiddleware
app = Application()
# Add the middleware
app.middlewares.append(RequestLoggingMiddleware(debug_mode=True))
# Your routes here...
Configuration
Basic Usage
from blacksheep_request_logger import RequestLoggingMiddleware
import logging
# Simple usage with debug mode
middleware = RequestLoggingMiddleware(debug_mode=True)
# Custom logger
logger = logging.getLogger("my_app.requests")
middleware = RequestLoggingMiddleware(logger=logger, debug_mode=True)
Advanced Configuration
middleware = RequestLoggingMiddleware(
debug_mode=True,
headers_to_log=["Accept", "Content-Type", "User-Agent", "X-Custom-Header"],
sensitive_headers={"authorization", "x-api-key", "cookie"},
log_level_request=logging.INFO,
log_level_response=logging.INFO,
log_level_error=logging.ERROR,
log_level_headers=logging.DEBUG,
)
Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
logger |
logging.Logger |
None |
Custom logger instance |
debug_mode |
bool |
False |
Enable header logging |
headers_to_log |
List[str] |
Common headers | Headers to log in debug mode |
sensitive_headers |
Set[str] |
Auth headers | Headers to redact |
log_level_request |
int |
INFO |
Log level for requests |
log_level_response |
int |
INFO |
Log level for responses |
log_level_error |
int |
ERROR |
Log level for errors |
log_level_headers |
int |
DEBUG |
Log level for headers |
Log Output
Normal Mode (Always logged)
→ GET /api/users
← GET /api/users - 200 (0.045s)
Debug Mode (Additional headers)
→ GET /api/users
Headers: {
"Accept": "application/json",
"User-Agent": "curl/7.68.0",
"Authorization": "[REDACTED]"
}
← GET /api/users - 200 (0.045s)
Integration Examples
With Environment-Based Configuration
import os
from blacksheep import Application
from blacksheep_request_logger import RequestLoggingMiddleware
app = Application()
# Enable debug mode based on environment
debug_mode = os.getenv("APP_ENV") == "development"
app.middlewares.append(
RequestLoggingMiddleware(debug_mode=debug_mode)
)
With Custom Logger Configuration
import logging
from blacksheep import Application
from blacksheep_request_logger import RequestLoggingMiddleware
# Configure custom logger
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
request_logger = logging.getLogger("myapp.requests")
app = Application()
app.middlewares.append(
RequestLoggingMiddleware(
logger=request_logger,
debug_mode=True
)
)
Production Configuration
from blacksheep_request_logger import RequestLoggingMiddleware
# Production-optimized setup
middleware = RequestLoggingMiddleware(
debug_mode=False, # No header logging
log_level_request=logging.INFO,
log_level_response=logging.WARNING, # Only log slow requests
sensitive_headers={
"authorization", "cookie", "x-api-key",
"x-auth-token", "session-id"
}
)
Security
The middleware automatically redacts sensitive headers:
AuthorizationCookieX-API-KeyX-Auth-Token
You can customize this by passing your own sensitive_headers set.
Performance
This middleware is designed for high performance:
- Pre-computed header lists
- Minimal memory allocations
- No regular expressions
- Efficient string formatting
- No request body reading
Typical overhead: < 0.1ms per request
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development
# Clone the repository
git clone https://github.com/kinsyudev/blacksheep-request-logger.git
cd blacksheep-request-logger
# Install with UV (recommended)
uv sync --dev
# Or install with pip
pip install -e ".[dev]"
# Run tests
uv run pytest
# or: pytest
# Format and lint code
uv run ruff format .
uv run ruff check --fix .
# Type checking
uv run mypy src/
# or: mypy src/
Quick Development Commands
For convenience, you can use the provided Makefile:
# Install dependencies
make install
# Run tests
make test
# Format code
make format
# Run all checks (lint, format check, type check)
make check
# Build package
make build
# Clean build artifacts
make clean
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
0.1.0
- Initial release
- Efficient request/response logging
- Configurable debug mode with header logging
- Automatic sensitive header redaction
- Full type hint support
Metadata
Release files for blacksheep-request-logger 0.1.0
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 |
|---|---|---|---|---|
| blacksheep_request_logger-0.1.0.tar.gz | 8.6 kB | Without provenance | Details for blacksheep_request_logger-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| blacksheep_request_logger-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for blacksheep_request_logger-0.1.0-py3-none-any.whl |
Total release size: 15.3 kB
Release files / blacksheep_request_logger-0.1.0.tar.gz
| Download URL | blacksheep_request_logger-0.1.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9876991ebc129bb57ae733ceb64503c8aa855352aba9319bbd21e5b8b57b9d15
|
|
BLAKE2b-256 checksum How to use checksums |
b2db9cc7389c23235bb9dfd6be6de10d2b430ca78a3db10d87f8b7b9236ddb03
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|
Release files / blacksheep_request_logger-0.1.0-py3-none-any.whl
| Download URL | blacksheep_request_logger-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5bcf536f3fb806477fc633280dde0718a2f4dbfa5385b4850fb9fc53107ecd0f
|
|
BLAKE2b-256 checksum How to use checksums |
3d318ccefb2ca62044b3ef18374a56509f7c5f2452608475fc30204ccf26bb7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|
Security for blacksheep-request-logger 0.1.0
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.