Skip to main content

Универсальная библиотека структурированного логирования на основе structlog

Project description

🐱 Lynx Logger

The all-in-one structured logging solution for modern Python applications.

Built on top of structlog. Designed for FastAPI, Flask, Django, and production scripts.

PyPI Version Python Versions License Downloads

FeaturesInstallationQuick StartConfigurationIntegrations


🚀 Features

Lynx Logger bridges the gap between simple print debugging and complex enterprise logging systems.

  • Zero-Config Start: Get beautiful logs with a single line of code.
  • 📦 Structured & JSON: Native JSON support for ELK Stack, Datadog, or Loki.
  • 🔍 Context-Aware: Automatic tracing of request_id, user_id, and trace contexts across your app.
  • 🛡 Smart Filtering: Built-in filters for PII (GDPR compliance), log levels, and sources.
  • 📂 File Rotation: Robust file handling with size limits and backups out of the box.
  • 🔌 Framework Ready: Middleware included for FastAPI, Flask, and Django.

📦 Installation

pip install lynx-logger

Optional Dependencies:

Extra Use Case
lynx-logger[web] Optimized for web frameworks (FastAPI, Starlette, Flask, Django)
lynx-logger[all] Installs all dependencies including dev tools

⚡ Quick Start

1. Basic Usage (Development)

Perfect for local development with readable, colored output.

from lynx_logger import setup_logger

logger = setup_logger("my_service")

logger.info("Service started", version="1.0.0")
logger.warning("Cache miss", key="user:123", latency_ms=45)
# Output: 2025-02-05 [INFO] my_service: Service started version=1.0.0

2. Production Setup (JSON)

Optimized for log aggregators.

logger = setup_logger(
    name="payment_service",
    level="INFO", 
    format="json",          # Outputs strict JSON
    log_to_file=True,
    logs_dir="./logs"
)

logger.info("Transaction processed", amount=500, currency="USD", user_id=42)
# Output: {"timestamp": "...", "level": "info", "event": "Transaction processed", "amount": 500, ...}

🎨 Output Formats

Format Example Output Best For
Console [INFO] app: Server started port=8000 (Colored) Local Development
JSON {"ts": "...", "level": "info", "msg": "Server started"} Production / ELK
Key-Value level=info event='Server started' port=8000 Legacy Systems

🧠 Context Management

Stop passing user_id as an argument to every function. Lynx Logger handles context for you.

Automatic Context (Context Manager)

from lynx_logger import RequestContext

# Automatically injects request_id into every log within this block
with RequestContext(request_id="req_123", user_id="user_456"):
    logger.info("Querying database") 
    # Log includes: request_id="req_123" user_id="user_456"

Context Binding

# Create a logger instance bound to specific data
job_logger = logger.bind(job_id="job_999")

job_logger.info("Job started") 
job_logger.info("Job finished")
# Both logs will contain job_id="job_999"

🛡 Advanced Filtering

Throttling (Rate Limiting)

Prevent log flooding when errors occur in a loop.

from lynx_logger import ThrottleFilter

# Allow max 10 identical messages per minute
throttle = ThrottleFilter(max_repeats=10, time_window=60)

Content Filtering (GDPR/Security)

Automatically mask or exclude sensitive data.

from lynx_logger import ContentFilter, LogConfig, LynxLogger

config = LogConfig(
    name="app",
    filters=ContentFilter(
        exclude_patterns=["password", "secret_key", "auth_token"],
        case_sensitive=False
    )
)
logger = LynxLogger(config)

⚙️ Configuration

Lynx Logger follows the 12-Factor App methodology and can be configured via Environment Variables.

Environment Variable Default Description
LOG_NAME root Service name
LOG_LEVEL INFO Logging level (DEBUG, INFO, ERROR)
LOG_FORMAT console Output format: console, json, keyvalue
LOG_TO_FILE false Enable file logging
LOG_DIR ./logs Directory for log files

Or via Python Dictionary:

config = LogConfig.from_dict({
    "name": "worker",
    "level": "DEBUG",
    "file": {
        "filename": "worker.log",
        "max_size": "50MB",
        "backup_count": 5
    }
})

🔌 Integrations

FastAPI Middleware Example

from fastapi import FastAPI, Request
from lynx_logger import setup_logger, RequestContext
import uuid

app = FastAPI()
logger = setup_logger("api", format="json")

@app.middleware("http")
async def log_middleware(request: Request, call_next):
    req_id = request.headers.get("X-Request-ID", str(uuid.uuid4()))
    
    # Context is automatically cleared after the request finishes
    with RequestContext(request_id=req_id, path=request.url.path):
        logger.info("Request started")
        response = await call_next(request)
        logger.info("Request finished", status=response.status_code)
        return response

🤝 Contributing

Contributions are welcome!

  1. Fork the repository.

  2. Create your feature branch.

  3. Commit your changes.

  4. Open a Pull Request.

Please open an Issue for any bugs or feature requests.

📄 License

This project is licensed under the MIT License.

Developed with ❤️ by FlacSy

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

lynx_logger-1.0.3.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

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

lynx_logger-1.0.3-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

Details for the file lynx_logger-1.0.3.tar.gz.

File metadata

  • Download URL: lynx_logger-1.0.3.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lynx_logger-1.0.3.tar.gz
Algorithm Hash digest
SHA256 54f41b9c263186a475d1f1d49cd323794ddc47b143c506786bc378b7b1c197f1
MD5 f49614f786b0cefe678336775cac576a
BLAKE2b-256 88a64849f9a7a7fcee4e0b8c7a5b72b96bfc91ed7536593f317972aaea8d649d

See more details on using hashes here.

File details

Details for the file lynx_logger-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: lynx_logger-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 42.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lynx_logger-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d8981ed5e6ef00e2148eb4debf8511aac3c09ed918bab86a0e1087341244d648
MD5 11abd94faacea5db8ccaaa8b491abf2a
BLAKE2b-256 2e68765ff0920b95d714e4758196e8ef98a12b46c495fdc14a92cdf6c73bdeae

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