Skip to main content

Rust RUST_LOG-style environment variable configuration for Python logging

Project description

Envlog

Tests Lint Code Quality Examples Python 3.8+ PyPI version License: MIT

Rust RUST_LOG-style environment variable configuration for Python's standard library logging.

Features

  • Simple: One environment variable configures all loggers
  • Familiar: Uses Rust's RUST_LOG syntax
  • Standard: Builds on Python's logging.config.dictConfig
  • Zero dependencies: Only uses Python standard library
  • Flexible: Works with loggers created anywhere in your codebase

Requirements

  • Python 3.8 or higher

Installation

pip install envlog

Quick Start

import logging
import envlog

# Initialize from PTHN_LOG environment variable
envlog.init()

# Now use logging normally
logger = logging.getLogger('myapp')
logger.info('Hello, world!')
# Set log level via environment
export PTHN_LOG=info
python myapp.py

Syntax

The log specification syntax follows Rust's RUST_LOG conventions:

# Set default level
PTHN_LOG=info

# Set module-specific levels
PTHN_LOG=myapp=debug

# Set default + module overrides
PTHN_LOG=warn,myapp=debug,otherlib=error

# Hierarchical modules (use . or :: separator)
PTHN_LOG=myapp.core=debug
PTHN_LOG=myapp::db=trace

# Complex example
PTHN_LOG=warn,myapp=info,myapp.db=debug,requests=error

Log Levels

Supports all standard levels (case-insensitive):

  • trace → Python's DEBUG (Rust's trace is more verbose, maps to Python's lowest)
  • debugDEBUG
  • infoINFO
  • warn / warningWARNING
  • errorERROR
  • criticalCRITICAL

Usage Examples

Basic Usage

import envlog

# Read from PTHN_LOG environment variable
envlog.init()

# Or specify configuration directly
envlog.init(log_spec='warn,myapp=debug')

# Or use custom environment variable name
envlog.init(env_var='MY_LOG')

Module-Specific Configuration

import logging
import envlog

envlog.init(log_spec='warn,myapp.core=debug,myapp.db=trace')

# Different modules get different log levels
core_logger = logging.getLogger('myapp.core')
core_logger.debug('Detailed debugging')  # Shows (DEBUG level)

db_logger = logging.getLogger('myapp.db')
db_logger.debug('Database query')  # Shows (TRACE->DEBUG level)

other_logger = logging.getLogger('requests')
other_logger.info('HTTP request')  # Hidden (WARN level)

Custom Formatting

envlog.init(
    log_spec='debug',
    log_format='%(levelname)s %(name)s: %(message)s',
    date_format='%H:%M:%S'
)

Force Reconfiguration

# First configuration
envlog.init(log_spec='info')

# Later, reconfigure (requires force=True)
envlog.init(log_spec='debug', force=True)

# Or use reset() then init()
envlog.reset()
envlog.init(log_spec='debug')

API Reference

envlog.init()

def init(
    log_spec: Optional[str] = None,
    env_var: str = 'PTHN_LOG',
    log_format: Optional[str] = None,
    date_format: Optional[str] = None,
    force: bool = False
) -> None

Initialize logging from a RUST_LOG-style specification.

Parameters:

  • log_spec: Explicit log specification (overrides env_var if provided)
  • env_var: Environment variable name to read (default: 'PTHN_LOG')
  • log_format: Custom log message format
  • date_format: Custom date format
  • force: Force reconfiguration even if already configured

envlog.reset()

def reset() -> None

Reset the configuration state, allowing init() to be called again without force=True.

envlog.parse_log_spec()

def parse_log_spec(spec: str) -> LogSpec

Parse a RUST_LOG-style specification into a LogSpec object. Useful for testing or advanced usage.

Comparison with RUST_LOG

Feature Rust RUST_LOG envlog
Default level RUST_LOG=info PTHN_LOG=info
Module-specific RUST_LOG=myapp=debug PTHN_LOG=myapp=debug
Module separator :: (e.g., myapp::core) . or :: (e.g., myapp.core)
Multiple modules RUST_LOG=warn,app=debug,lib=error Same
Trace level Separate from debug Maps to DEBUG

How It Works

  1. Parses RUST_LOG-style specification
  2. Converts to Python logging level names
  3. Generates a logging.config.dictConfig configuration
  4. Applies configuration to Python's standard library logging

The logging configuration uses:

  • Console handler writing to stderr
  • Standard formatter with timestamp, level, logger name, and message
  • Non-destructive configuration (doesn't disable existing loggers)

Development

# Clone repository
git clone https://github.com/bassmanitram/python-envlog.git
cd envlog

# Install in development mode
pip install -e ".[dev,test]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=envlog --cov-report=html

License

MIT License - see LICENSE file for details

Contributing

Contributions welcome! Please open an issue or pull request.

For Developers and AI Agents

See AGENT_BOOTSTRAP.md for comprehensive project documentation including:

  • Complete architecture overview
  • Module-by-module code walkthrough
  • Testing strategies and guidelines
  • CI/CD pipeline details
  • Development workflow and conventions
  • Design decisions and rationale

This document provides the technical context needed to understand and contribute to the project effectively.

Acknowledgments

Inspired by Rust's env_logger crate and the RUST_LOG convention.

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

envlog-1.0.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

envlog-1.0.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file envlog-1.0.0.tar.gz.

File metadata

  • Download URL: envlog-1.0.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for envlog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e354f770b6f97b24edd5fc4a44c4db0ec9a048bb93074c4854ad6171f6fa0356
MD5 5ffcd20fae190a2ef058e9d7ad49b604
BLAKE2b-256 e910dbfe276493e567e6013adf498042ab774faf8cb472ff81898b7b83f8af24

See more details on using hashes here.

Provenance

The following attestation bundles were made for envlog-1.0.0.tar.gz:

Publisher: release.yml on bassmanitram/python-envlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file envlog-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: envlog-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for envlog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a4b255b307d7a9fb00558447baa26ca14a57760d1d78f70a3f102220e0bb674
MD5 87ced30beca13ad956fdd9bc2d7c482d
BLAKE2b-256 19250cca39fb9b56ef978f193c3438a1fae347eda3ec5f911bf05292553695f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for envlog-1.0.0-py3-none-any.whl:

Publisher: release.yml on bassmanitram/python-envlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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