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
| 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=Truemasks 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.ContextFilterskips context keys that conflict with standardLogRecordfields (e.g.,msg,levelname) and emits awarnings.warn.PyLogShieldMiddlewaresanitizes the incomingX-Request-IDheader 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!
๐ Full Docs ย ยทย ๐ง Report a Bug ย ยทย โช Vertex AI Automations
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e627810f20c4f06a67b0711c9f6d5330f99543dd028abf54e8e348626cda24e6
|
|
| MD5 |
267c4f6d1d4042540d39918ce5322f1a
|
|
| BLAKE2b-256 |
4f66683b50b2bd32209d977edd1b13fc46b6ae2da5ce17cdd54846577511021b
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b17b05284322e1f26623a84efc34d1e64754233a1fe269cf887b504bd1ca7f4
|
|
| MD5 |
d5d8298d43da2b00121a95d4c1eeba57
|
|
| BLAKE2b-256 |
f36e133c33d22dafe8288150f8b948cae4c6943461f38fb471644b73e1d4fe67
|