Rust RUST_LOG-style environment variable configuration for Python logging
Project description
envlog
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'sDEBUG(Rust's trace is more verbose, maps to Python's lowest)debug→DEBUGinfo→INFOwarn/warning→WARNINGerror→ERRORcritical→CRITICAL
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 formatdate_format: Custom date formatforce: 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
- Parses RUST_LOG-style specification
- Converts to Python logging level names
- Generates a
logging.config.dictConfigconfiguration - 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/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.
Acknowledgments
Inspired by Rust's env_logger crate and the RUST_LOG convention.
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 envlog-0.1.0.tar.gz.
File metadata
- Download URL: envlog-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12a877c5b86c053e902a3077d8bb51a0c2f592827e7d9a7d659750944ee4a605
|
|
| MD5 |
55608aa259247d8a5b03eb3a95fb18bb
|
|
| BLAKE2b-256 |
3e271f5904162df466a8f474e7f7585053e035c6bbd604cedd167d0eccda6845
|
Provenance
The following attestation bundles were made for envlog-0.1.0.tar.gz:
Publisher:
release.yml on bassmanitram/python-envlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envlog-0.1.0.tar.gz -
Subject digest:
12a877c5b86c053e902a3077d8bb51a0c2f592827e7d9a7d659750944ee4a605 - Sigstore transparency entry: 708625877
- Sigstore integration time:
-
Permalink:
bassmanitram/python-envlog@21347d733b63f200d44f4d96d9359da268bd65af -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bassmanitram
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@21347d733b63f200d44f4d96d9359da268bd65af -
Trigger Event:
push
-
Statement type:
File details
Details for the file envlog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: envlog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
040a79fe89cb7d54090bac85d0e3260e7d9ac3b95c76b0db0c06dd322dd6edd9
|
|
| MD5 |
1b0070459cdbe083d50453d66e14fc5e
|
|
| BLAKE2b-256 |
c95abb68c7635411a4208b46005406495cbdb94215845df7aceeadacb69be8b7
|
Provenance
The following attestation bundles were made for envlog-0.1.0-py3-none-any.whl:
Publisher:
release.yml on bassmanitram/python-envlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envlog-0.1.0-py3-none-any.whl -
Subject digest:
040a79fe89cb7d54090bac85d0e3260e7d9ac3b95c76b0db0c06dd322dd6edd9 - Sigstore transparency entry: 708625880
- Sigstore integration time:
-
Permalink:
bassmanitram/python-envlog@21347d733b63f200d44f4d96d9359da268bd65af -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bassmanitram
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@21347d733b63f200d44f4d96d9359da268bd65af -
Trigger Event:
push
-
Statement type: