Skip to main content

Structured colored logger with trace/span context and sampling

Project description

Project logger

A production-ready Python logging framework designed for modern backends and AI pipelines.

It supports:

  • Structured JSON logs (ELK / GCP Logging / Datadog ready)
  • Trace + Span IDs (contextvars, async-safe)
  • Automatic context (file:line Class.method())
  • Per-module log levels
  • Sampling filters for hot paths (reduce noise + cost)

Table of Contents


Overview

This project provides a clean, extensible logging layer for Python services.

It’s built to solve common production pain points:

  • Logs without structure are hard to search
  • Async + threads break context
  • Trace correlation is missing
  • Hot loops spam logs and increase cloud costs
  • Large codebases need per-module control

This logger provides a single unified API for:

  • console logs (pretty + colored)
  • file logs (JSON structured)
  • trace/span correlation
  • sampling

Architecture

LoggerConfig
    ↓
Logger (singleton)
    ↓
logging.Logger (stdlib)
    ↓
Handlers
    ├── Console handler (colored)
    └── File handler (.log or .json)
    ↓
Filters
    └── SamplingFilter
    └── DeterministicFilter
    ↓
ContextLogger (LoggerAdapter)
    ├── context
    ├── trace_id
    └── span_id

Core Components:

  1. API Layer: Interfaces (CLI, SDK, REST) for uploading and managing files
  2. Service Layer: Business logic for versioning and lifecycle rules
  3. Repository Layer: Abstract persistence interfaces for storage and metadata
  4. Infrastructure Layer: Cloud storage providers and NoSQL database adapters

Features

  1. Structured JSON logging

    • Compatible with ELK, GCP Logging, Datadog, Splunk
    • Includes context, trace_id, span_id, module, function, line, exception
  2. Trace + Span IDs

    • Uses contextvars (async-safe)
    • Supports manual trace/span injection
  3. Automatic context resolution

    • Adds file:line Class.method() automatically
    • Works for functions, methods, decorators
  4. Per-module log levels

    • Example: src.core=WARNING while the rest stays INFO
  5. Sampling filter

    • Sample DEBUG/INFO logs for noisy paths
    • Always keep WARNING/ERROR/CRITICAL
  6. Singleton logger factory

    • Global configuration
    • Safe Logger.configure(...) entrypoint

Installation

pip install <url>

Or install from source:

git clone <url>
cd project-name
pip install -e .

Usage

Basic setup

import logging
from src.logger import Logger
from src.config import LoggerConfig

Logger.configure(
    LoggerConfig(
        name="app",
        level=logging.INFO,
        directory="logs",
        json_logs=True,
        sample_rate=0.2,
        module_levels={
            "src.core": logging.WARNING,
        },
    )
)

logger = Logger().bind("startup")
logger.info("Service initialized")

Trace + ID span

from src.logger import Logger

log = Logger()
log.set_trace()
log.set_span()

logger = log.bind("request")
logger.info("Request started")

JSON logs example output

{
  "timestamp": "2026-02-04T10:55:01.140Z",
  "level": "INFO",
  "message": "Request started",
  "logger": "app",
  "context": "api.py:88 EDDController.calculate()",
  "trace_id": "a1f29c...",
  "span_id": "91c77d...",
  "module": "api",
  "function": "calculate",
  "line": 88
}

Configuration

Configuration is done through LoggerConfig.

Example:

from src.config import LoggerConfig

config = LoggerConfig(
    name="service",
    directory="logs",
    json_logs=True,
    sample_rate=0.1,
    level=logging.INFO,
    module_levels={
        "src.core": logging.WARNING,
        "google": logging.ERROR,
        "httpx": logging.ERROR,
    },
)

Key settings

Field Meaning
json_logs Output JSON logs for file handler
directory Where log files are stored
deterministic Enable deterministic logging
sample_rate Sampling probability for INFO/DEBUG
module_levels Per-module log level override
level Global log level
name Logger name

Project Structure

src/
 ├── logger.py              # Logger singleton + ContextLogger
 ├── config.py              # LoggerConfig + JSONFormatter + SamplingFilter
 ├── core/
 │    ├── filters.py        # Log filtering logic
 │    ├── formatters.py     # Log formatting logic
 │    └── tracer.py         # contextvars trace_id/span_id
 └── decorators/
      ├── functions.py      # function_log decorator
      └── classes.py        # class_log decorator


Development

pip install -r requirements-dev.txt

Testing

pytest tests/

Deployment

The service can be deployed as:

Python SDK library
FastAPI microservice
Serverless function (Cloud Run / Lambda / Azure Functions)
Internal data platform component

Roadmap

  1. Request middleware integration for Flask/FastAPI
  2. Deterministic sampling by trace_id (avoid partial traces)
  3. Rotating file handler support
  4. OpenTelemetry bridge
  5. Log batching / async writer for high throughput

Contributing

Contributions are welcome. Please follow the coding standards and submit PRs with tests and documentation updates.


License

MIT License.


Contact

Maintainer: Evan Flores Email: efloresp06@liverpool.com.mx Organization: Liverpool

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

evan_logger-0.1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

evan_logger-0.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file evan_logger-0.1.1.tar.gz.

File metadata

  • Download URL: evan_logger-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for evan_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f1edb541a4a872cfa5e9e30c0cd48bfc369281a7e8bc69f39a19a5f3c9dd357c
MD5 6ad2ab42c2f6ae88874b38f35f5291db
BLAKE2b-256 5b8d14cf6fbbd8cf15ea90ca5cbc853be3e05f02d8eeaf9e3725d865bcd88a80

See more details on using hashes here.

File details

Details for the file evan_logger-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: evan_logger-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for evan_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62602f414f2258facd6c61902fd7d2003ff5dce3c6186edd1326c6aae4d5fc1a
MD5 cec32d3fc6fd08d0775748205dc6b44a
BLAKE2b-256 11ec92c16cc4a35afd95804faca1309c40e3b604a41cb0a7cf4ab36c95fdb004

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