Skip to main content

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 RotatingFileHandler when 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 pycloudwatch

🛠 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, and logging.handlers (Python logging ecosystem)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudwatchpy-0.0.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudwatchpy-0.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file cloudwatchpy-0.0.1.tar.gz.

File metadata

  • Download URL: cloudwatchpy-0.0.1.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

Hashes for cloudwatchpy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c70bfdc7c43e3ebcd36782d3d23135415ec33771c990d54bbf430b7b7620b124
MD5 2653d9cf2d480ed6e1ff5d823d398307
BLAKE2b-256 28ac95db5deb7b26a4b323b5de2fa64d297dab920857cabdd73a03f6e4c252e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudwatchpy-0.0.1.tar.gz:

Publisher: python-publish.yml on Irfan-Ahmad-byte/cloudwatchpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cloudwatchpy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: cloudwatchpy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cloudwatchpy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0281aeaacd2182e8287d4a67134ee38cce66b56c8d6053722718f42fba2b597c
MD5 0815b05049dfd839fc826633e2a403a8
BLAKE2b-256 15b89dda6c477ea31d3368dc51fc5ac8fa615b71c1ddd3ddcbd724fd5093148c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudwatchpy-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on Irfan-Ahmad-byte/cloudwatchpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page