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 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, 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.2.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.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

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

Hashes for cloudwatchpy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 955f25a34c838e6604813e74da33a629a780d13768ba0ec1db152b9e03aec713
MD5 e7f715546dab04cd7ed23a6e7b996cf9
BLAKE2b-256 2a274e0b8357517ac76eed0bd70c1f7c13e6cc00807765943371703a7733e065

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudwatchpy-0.0.2.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.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

Hashes for cloudwatchpy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c7316fa72d24c310073fef2b13aa163157e6f946958c25538f6136e245b2f399
MD5 f401cc67ec93bc1e7bc506fc97ac619a
BLAKE2b-256 54f00cf6d643d9d1c11c7a3c9b3caf03941ba7bdcfc64b016ad2751602f557f9

See more details on using hashes here.

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

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