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
๐Ÿ–ฅ๏ธ Interactive TUI Full-screen viewer with live search, filters, export (CSV/JSON/HTML), and live-follow mode
๐Ÿงต 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

๐Ÿ–ฅ๏ธ Interactive TUI Viewer

Install the optional TUI extra and launch the full-screen interactive log viewer:

pip install "pylogshield[tui]"

pylogshield tui --file ~/.logs/myapp.log
pylogshield tui --file app.log --level ERROR        # start with ERROR+ filter
pylogshield tui --file app.log --follow             # start in live-follow mode

PyLogShield Interactive TUI Viewer

Key Action
/ Focus search bar โ€” filters rows as you type, highlights matches
Ctrl+F Open filter panel โ€” level toggles, time range, logger name
F Toggle live-follow mode โ€” auto-pauses when you scroll up
E Export current filtered view to CSV, JSON, plain text, or HTML
? Show keyboard reference
Q Quit

๐Ÿ–ฅ๏ธ 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.2.1.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

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

pylogshield-0.2.1-py3-none-any.whl (49.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pylogshield-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e627810f20c4f06a67b0711c9f6d5330f99543dd028abf54e8e348626cda24e6
MD5 267c4f6d1d4042540d39918ce5322f1a
BLAKE2b-256 4f66683b50b2bd32209d977edd1b13fc46b6ae2da5ce17cdd54846577511021b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylogshield-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 49.5 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b17b05284322e1f26623a84efc34d1e64754233a1fe269cf887b504bd1ca7f4
MD5 d5d8298d43da2b00121a95d4c1eeba57
BLAKE2b-256 f36e133c33d22dafe8288150f8b948cae4c6943461f38fb471644b73e1d4fe67

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