Skip to main content

A Python logging library designed for data professionals and developers who need reliable, secure logging with minimal setup.

Project description


๐Ÿ“‹ Table of Contents


๐Ÿ“ฃ Overview

PyLogShield is a Python logging library for data professionals and developers who need reliable, secure logging with minimal setup. It extends Python's standard logging module with production-ready features: sensitive data masking, rate limiting, async logging, JSON formatting, context propagation, FastAPI middleware, and a rich CLI log viewer.

Your App  โ”€โ”€โ–บ  PyLogShield  โ”€โ”€โ–บ  Masking  โ”€โ”€โ–บ  Rate Limiter  โ”€โ”€โ–บ  Context  โ”€โ”€โ–บ  Handlers
                                                                              โ”œโ”€โ”€ Console / Rich
                                                                              โ”œโ”€โ”€ File / Rotating
                                                                              โ”œโ”€โ”€ JSON
                                                                              โ””โ”€โ”€ Async Queue

๐Ÿ’ก Features

Feature Description
๐Ÿ”’ Sensitive Data Masking Masks passwords, tokens, API keys in strings, dicts, and exception args
๐Ÿšฆ Rate Limiting Suppresses duplicate messages within a configurable time window
๐Ÿ“„ JSON Formatting Structured ISO 8601 JSON for ELK, Splunk, CloudWatch
๐Ÿ”„ Log Rotation Size-based rotation with configurable backup counts
โšก Async Logging Non-blocking background queue with configurable max size
๐ŸŽจ Rich Console Color-coded terminal output via the rich library
๐Ÿ” CLI Log Viewer Static and live-follow modes with level/keyword filtering
๐Ÿงต Context Propagation Thread-safe and asyncio-safe structured field injection
๐ŸŒ FastAPI Middleware Auto-injects request_id, method, path, and client IP
๐Ÿ“Š Metrics Per-level log counts and logs/second tracking
โ˜๏ธ Cloud Scrubbing Strips AWS_, AZURE_, GCP_, GOOGLE_, TOKEN prefixed fields
๐Ÿท๏ธ Custom Log Levels Register SECURITY, AUDIT, TRACE levels at runtime

๐Ÿ“Œ Quick Start

Installation

pip install pylogshield

# FastAPI middleware support
pip install "pylogshield[fastapi]"

Basic Usage

from pylogshield import get_logger

logger = get_logger("my_app", log_level="INFO")

logger.info("Application started")
logger.warning("Low memory detected")
logger.error("Connection failed")

# Sensitive data is masked with mask=True
logger.info({"user": "john", "api_key": "sk-1234567890"}, mask=True)
# โ†’ {"user": "john", "api_key": "***"}

Production Setup

from pylogshield import get_logger, add_sensitive_fields
from pylogshield.context import log_context

add_sensitive_fields(["ssn", "credit_card"])

logger = get_logger(
    "production_app",
    log_level="INFO",
    enable_json=True,            # Structured JSON
    rotate_file=True,            # Auto-rotate log files
    rotate_max_bytes=10_000_000, # 10 MB per file
    rate_limit_seconds=0.5,      # Prevent log flooding
    use_queue=True,              # Non-blocking async logging
    queue_maxsize=50_000,        # Cap memory usage
    enable_metrics=True,         # Track log statistics
    enable_context=True,         # Structured context injection
)

with log_context(request_id="abc-123", user_id=42):
    logger.info("Processing payment", mask=True)
    # โ†’ {"message": "Processing payment", "request_id": "abc-123", "user_id": 42}

FastAPI Middleware

from fastapi import FastAPI
from pylogshield import get_logger
from pylogshield.middleware import PyLogShieldMiddleware

app = FastAPI()
logger = get_logger("api", enable_context=True, enable_json=True)
app.add_middleware(PyLogShieldMiddleware, logger=logger)

# Every log line automatically includes: request_id, http_method, http_path, client_ip

๐Ÿ–ฅ๏ธ Terminal Demo

Sensitive Data Masking โ€” Before vs After

# Without mask=True
INFO  my_app  {"user": "john", "password": "hunter2", "token": "eyJhbGci..."}

# With mask=True  โœ“
INFO  my_app  {"user": "john", "password": "***", "token": "***"}

JSON Output

{
  "timestamp": "2026-03-14T10:30:00.123+00:00",
  "host": "prod-server-01",
  "logger": "production_app",
  "level": "INFO",
  "message": "Processing payment",
  "request_id": "abc-123",
  "user_id": 42
}

CLI Log Viewer

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Log Viewer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ File:    /var/log/app/production.log               โ”‚
โ”‚ Limit:   200 lines                                 โ”‚
โ”‚ Level:   ERROR                                     โ”‚
โ”‚ Keyword: "timeout"                                 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Timestamp                โ”‚ Level    โ”‚ Message                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2026-03-14T10:28:11.001  โ”‚ ERROR    โ”‚ DB timeout after 30s      โ”‚
โ”‚ 2026-03-14T10:29:54.332  โ”‚ ERROR    โ”‚ Connection timeout: redis  โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
                          2 entries shown
# Follow live logs
pylogshield follow -f app.log -l ERROR -k "timeout"

# View last 50 lines
pylogshield view -f app.log -n 50 -l WARNING

# List log levels
pylogshield levels

โš ๏ธ Security Notes

  • mask=True masks sensitive values in strings, dicts, and exception .args. Traceback source lines are not redacted โ€” they reflect the literal source text. Avoid storing sensitive values in local variables inside functions that may raise logged exceptions.
  • ContextFilter skips context keys that conflict with standard LogRecord fields (e.g., msg, levelname) and emits a warnings.warn.
  • PyLogShieldMiddleware sanitizes the incoming X-Request-ID header before injecting it into logs (truncated to 128 chars, non-alphanumeric characters stripped).

๐Ÿ‘ช Contributing

All contributions are welcome! Fork the repo, make your changes, and open a pull request. You can also open an issue with the label enhancement.

Don't forget to โญ star the project!

๐Ÿ”ถ View all contributors


๐Ÿ“ƒ Full Docs ย ยทย  ๐Ÿ”ง Report a Bug ย ยทย  โ›ช Vertex AI Automations

(back to top)

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

pylogshield-0.0.7.tar.gz (563.6 kB view details)

Uploaded Source

Built Distribution

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

pylogshield-0.0.7-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

Details for the file pylogshield-0.0.7.tar.gz.

File metadata

  • Download URL: pylogshield-0.0.7.tar.gz
  • Upload date:
  • Size: 563.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pylogshield-0.0.7.tar.gz
Algorithm Hash digest
SHA256 5a7e45c6443a535591ebc71da7ca75a866da31e0cb8e2889478fc720720c1161
MD5 61de2c3f1264219c21e5f4b04c3788df
BLAKE2b-256 7f71b95fe06e5275245e2dad26da6f554f816b74dcbf294fb2b717837e3e9ce9

See more details on using hashes here.

File details

Details for the file pylogshield-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: pylogshield-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pylogshield-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0efb68f18216368ee70862c1b6ba11ae2c33cf841db24aa3395dbd5b0b37f46d
MD5 9d9e130fc0bebe7d7c97d86c9cffaa91
BLAKE2b-256 d648672a9a65de49af417afead180662a74a3dbf473039fcf50e4cfc6eec0e98

See more details on using hashes here.

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