Skip to main content

Python bindings for the Polyglot native structured logger

Project description

Polyglot Logger (Python)

Idiomatic Python bindings (polyglot-logger) for the Polyglot native structured logger.

Part of the Polyglot modular monorepo — see polyglot-go for the core Go implementation and other language bindings.

Quick Start

from polyglot_logger import Logger, Level

with Logger("payments-api", environment="prod", file_path="/var/log/payments.log") as log:
    log.set_fields({"traceId": "abc"})
    log.info("order created", order_id=123, amount=42.5)
    log.log_simple(Level.ERROR, "payment failed")
    print(log.stats())

Features

  • Auto-initialization: Place polyglot.yaml in project root, logger auto-discovers & loads it
  • Bundled binaries: Pre-compiled native libraries for Windows/macOS/Linux included in wheel
  • Zero config: Works immediately after pip install without manual setup
  • Thread-safe: Concurrent log/flush/stats/set_fields calls safe on one instance
  • Type hints: Full PEP 484 annotations in source

Installation

pip install polyglot-logger

All native binaries (.so, .dll, .dylib) are included in the wheel. No separate compilation needed.

Building from Source

To build from the core repository with all language bindings:

git clone --recurse-submodules https://github.com/kishankumarhs/Polyglot.git
cd Polyglot

# Build native library and all bindings
make build-native
pip install -e bindings/python

Configuration

Place a polyglot.yaml in your project root:

service: payments-api
environment: prod
logging:
  level: info
  async: true
file:
  enabled: true
  path: /var/log/payments.log

On first import, the logger auto-discovers this config and initializes automatically.

Alternatively, set environment variables:

export POLYGLOT_CONFIG_PATH=/path/to/polyglot.yaml
export POLYGLOT_CONFIG_FILE=/path/to/config.json

Documentation

Resource Description
Python API Language-specific API reference
User Guide Logging concepts, fields, async modes
Configuration Full schema & examples
Getting Started Build & run first log
Architecture Design & internals
Repositories Overview How all 4 repos work together

Usage Examples

Basic Logging

from polyglot_logger import Logger, Level

log = Logger("my-app", environment="prod")
log.info("Application started")
log.error("Something went wrong", error="connection timeout")
log.close()

With Context Fields

log.set_fields({"user_id": "u123", "request_id": "r456"})
log.info("user action")  # Includes user_id & request_id automatically

Multiple Sinks

log = Logger(
    service="payments-api",
    environment="prod",
    file_path="/var/log/payments.log",
    http_endpoint="https://logs.company.com/ingest",
)
log.info("payment processed")  # Sent to both file and HTTP endpoint

Statistics

stats = log.stats()
print(stats.queued)    # Entries currently in queue
print(stats.flushed)   # Entries successfully written
print(stats.dropped)   # Entries dropped due to overflow
print(stats.errors)    # Write errors

Thread Safety

A single logger instance is safe for concurrent calls to:

log.log_simple(level, message)  # Thread-safe
log.info(msg, **fields)         # Thread-safe
log.flush()                     # Thread-safe
log.stats()                     # Thread-safe
log.set_fields(fields)          # Thread-safe
log.reload_config()             # Thread-safe

Only close() should be called by a single thread (one owner of the logger lifecycle).

Native Library Auto-Discovery

The binding automatically discovers the native library:

  1. Checks for bundled binary in polyglot_logger/bin/
  2. Falls back to POLYGLOT_LOGGER_LIB environment variable (if set)
  3. If neither found: error with helpful message

Troubleshooting

"native library not found"

# Verify package has binaries
pip show polyglot-logger | grep Location
# Check if bin/ folder exists
ls ~/.venv/lib/python3.x/site-packages/polyglot_logger/bin/

# Set explicit path
export POLYGLOT_LOGGER_LIB=/path/to/liblogger.so

"polyglot.yaml not found"

# Create config in your project root
touch polyglot.yaml

# Or set environment variable
export POLYGLOT_CONFIG_PATH=/path/to/config.yaml

ImportError on macOS

Ensure you're using Python 3.9+:

python3 --version
pip install --upgrade polyglot-logger

Version Management

This binding is independently versioned. Each release:

  • Includes latest C ABI from polyglot-go
  • Pre-compiled native binaries for all platforms
  • Complete source with type hints

Check polyglot-go releases to see which core version this binding is based on.

Repository Links

Contributing

To contribute to Python bindings:

# Clone core with all submodules
git clone --recurse-submodules https://github.com/kishankumarhs/Polyglot.git
cd Polyglot/bindings/python

# Create virtual environment
python -m venv venv
source venv/bin/activate

# Install in development mode
pip install -e .

# Make changes
vim polyglot_logger/__init__.py

# Run tests
python -m pytest tests/

# Commit and push to polyglot-py
git add polyglot_logger/
git commit -m "fix: description"
git push origin main

# Update core submodule reference
cd ../..
git add bindings/python
git commit -m "chore: bump python binding"
git push origin main

License

MIT — see LICENSE in this repository

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

polyglot_logger-0.3.0.tar.gz (31.7 MB view details)

Uploaded Source

Built Distribution

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

polyglot_logger-0.3.0-py3-none-any.whl (31.8 MB view details)

Uploaded Python 3

File details

Details for the file polyglot_logger-0.3.0.tar.gz.

File metadata

  • Download URL: polyglot_logger-0.3.0.tar.gz
  • Upload date:
  • Size: 31.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for polyglot_logger-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a679461721ccfacfde38bb8e9797245a6f54826d8d900dc24058dea227ba70a5
MD5 b415fcad063b17febbc6b083637ee3c4
BLAKE2b-256 26f6571e39d7ac600a6c83bf4f32ed9df5cd2532201388a6d98914247b01ee3c

See more details on using hashes here.

File details

Details for the file polyglot_logger-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for polyglot_logger-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f8471ae571baca1387b7bf9678e6e431f8ab656c86e32e70f689717cfba625a
MD5 d668f1cf8a5a8ad0695720e0e8201c60
BLAKE2b-256 516af0465c57a2ae587724b2bb3a90a4be0cde3455bda47366b63f0394ee346a

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