Structured Python logger for AWS CloudWatch with ASGI/WSGI middleware support and fallback local logging
Project description
📡 pycloudwatch
Structured Python logger with AWS CloudWatch support and a fallback to local JSON logging.
🔥 Features
- ✅ Seamless AWS CloudWatch Logs integration (via
boto3) - ✅ Automatic fallback to
RotatingFileHandlerwhen CloudWatch config is missing - ✅ ASGI middleware for FastAPI, Starlette, etc.
- ✅ Log batching with thread-safe flushes
- ✅ JSON-structured logs, optionally compressible
- ✅ Plug-and-play logger interface
🚀 Installation
pip install cloudwatchpy
🛠 Environment Configuration
| Variable | Description |
|---|---|
AWS_ACCESS_KEY_ID |
AWS access key |
AWS_SECRET_ACCESS_KEY |
AWS secret key |
AWS_REGION |
AWS region (default: us-east-1) |
CLOUDWATCH_LOG_GROUP |
Name of CloudWatch log group |
CLOUDWATCH_LOG_STREAM |
Name of CloudWatch log stream |
LOG_BATCH_SIZE |
Batch flush size (default: 10) |
LOG_COMPRESSION |
Enable GZIP compression (true/false) |
If any of these are missing, logs will fallback to local logs/app.log.
🧱 Basic Usage
from pycloudwatch.logger import PyCloudLogger
logger = PyCloudLogger("my_app")
logger.info("Server started", context={"port": 8000})
logger.error("Failed to connect to DB", context={"error": "Timeout"})
🧩 ASGI Middleware (FastAPI Example)
from fastapi import FastAPI
from pycloudwatch.middleware import CloudWatchMiddleware
app = FastAPI()
app.add_middleware(CloudWatchMiddleware)
📦 Batching Logs (Advanced)
from pycloudwatch.batcher import LogBatcher
from pycloudwatch.models import LogEntry
from pycloudwatch.logger import PyCloudLogger
logger = PyCloudLogger("batcher-demo")
batcher = LogBatcher(logger.backend, batch_size=5)
for i in range(10):
batcher.add_log(LogEntry("INFO", f"Batch Log {i+1}", {"iteration": i+1}))
batcher.flush()
🧪 Testing
To run tests:
pytest tests/
Mocks for AWS SDK via moto will be included in the test suite.
💡 Roadmap
- WSGI middleware support
- Automatic periodic flush using timer
- Integration with external S3/Kinesis log exporters
- OpenTelemetry context propagation
📜 License
MIT © 2025 Irfan Ahmad
🧠 Inspired by
crypsol_logger(Rust)structlog,loguru, andlogging.handlers(Python logging ecosystem)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cloudwatchpy-0.0.2.tar.gz.
File metadata
- Download URL: cloudwatchpy-0.0.2.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
955f25a34c838e6604813e74da33a629a780d13768ba0ec1db152b9e03aec713
|
|
| MD5 |
e7f715546dab04cd7ed23a6e7b996cf9
|
|
| BLAKE2b-256 |
2a274e0b8357517ac76eed0bd70c1f7c13e6cc00807765943371703a7733e065
|
Provenance
The following attestation bundles were made for cloudwatchpy-0.0.2.tar.gz:
Publisher:
python-publish.yml on Irfan-Ahmad-byte/cloudwatchpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cloudwatchpy-0.0.2.tar.gz -
Subject digest:
955f25a34c838e6604813e74da33a629a780d13768ba0ec1db152b9e03aec713 - Sigstore transparency entry: 210653704
- Sigstore integration time:
-
Permalink:
Irfan-Ahmad-byte/cloudwatchpy@6a2597a1b9e938b860829d82e123b3093c656212 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Irfan-Ahmad-byte
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6a2597a1b9e938b860829d82e123b3093c656212 -
Trigger Event:
release
-
Statement type:
File details
Details for the file cloudwatchpy-0.0.2-py3-none-any.whl.
File metadata
- Download URL: cloudwatchpy-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7316fa72d24c310073fef2b13aa163157e6f946958c25538f6136e245b2f399
|
|
| MD5 |
f401cc67ec93bc1e7bc506fc97ac619a
|
|
| BLAKE2b-256 |
54f00cf6d643d9d1c11c7a3c9b3caf03941ba7bdcfc64b016ad2751602f557f9
|
Provenance
The following attestation bundles were made for cloudwatchpy-0.0.2-py3-none-any.whl:
Publisher:
python-publish.yml on Irfan-Ahmad-byte/cloudwatchpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cloudwatchpy-0.0.2-py3-none-any.whl -
Subject digest:
c7316fa72d24c310073fef2b13aa163157e6f946958c25538f6136e245b2f399 - Sigstore transparency entry: 210653705
- Sigstore integration time:
-
Permalink:
Irfan-Ahmad-byte/cloudwatchpy@6a2597a1b9e938b860829d82e123b3093c656212 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/Irfan-Ahmad-byte
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6a2597a1b9e938b860829d82e123b3093c656212 -
Trigger Event:
release
-
Statement type: