Structured logging library for Python applications
Project description
PyLa Logger
A structured logging library for Python applications that provides JSON-formatted logs with context management and flexible configuration.
Features
- Structured JSON Logging: All logs are output in JSON format with ISO timestamps
- Context Management: Add persistent context data that appears in all subsequent log messages
- Environment-Based Configuration: Control log levels via environment variables
- Standard Log Levels: Support for debug, info, warning, error, and critical levels
- Exception Logging: Special handling for exceptions with stack trace information
- Built on Structlog: Leverages the powerful and reliable structlog library
- Ready to Use: Pre-configured logger instance available for immediate use
Installation
pip install pyla-logger
Quick Start
from pyla_logger import logger
# Basic logging
logger.info("Application started")
logger.error("Something went wrong", error_code=500)
# Add persistent context
logger.add_context(user_id="12345", session="abc-def")
logger.info("User action") # Will include user_id and session in output
# Exception logging
try:
raise ValueError("Invalid input")
except Exception as e:
logger.exception(e, "Failed to process request")
Configuration
Log Levels
Control the minimum log level using the pyla_logger_level environment variable:
export pyla_logger_level=info # Only info, warning, error, and critical logs will be shown
export pyla_logger_level=error # Only error and critical logs will be shown
Supported levels (case-insensitive):
debug(default)infowarningerrorcritical
Usage Examples
Basic Logging
from pyla_logger import logger
logger.debug("Detailed debug information")
logger.info("General information", component="auth")
logger.warning("Warning message", retry_count=3)
logger.error("Error occurred", error_type="validation")
logger.critical("Critical system failure", system="database")
Context Management
from pyla_logger import logger
# Add context that will appear in all subsequent logs
logger.add_context(
request_id="req-12345",
user_id="user-67890",
environment="production"
)
logger.info("Processing request")
# Output: {"event": "Processing request", "request_id": "req-12345", "user_id": "user-67890", "environment": "production", "timestamp": "2024-01-01T12:00:00.000000Z", "level": "info"}
logger.error("Request failed", error_code=400)
# Output: {"event": "Request failed", "error_code": 400, "request_id": "req-12345", "user_id": "user-67890", "environment": "production", "timestamp": "2024-01-01T12:00:01.000000Z", "level": "error"}
Exception Logging
from pyla_logger import logger
try:
result = 10 / 0
except Exception as e:
logger.exception(e, "Division operation failed", operation="divide", operands=[10, 0])
# Includes full stack trace and exception details
Advanced Usage
from pyla_logger import logger
# Multiple arguments and keyword arguments
logger.info("User logged in", "additional", "arguments",
user_name="john_doe", ip_address="192.168.1.1", success=True)
# Adding context incrementally
logger.add_context(service="auth-service")
logger.add_context(version="1.2.3") # Adds to existing context
logger.info("Service status check") # Contains all context data
Creating Custom Logger Instances
from pyla_logger.logger import Logger
import structlog
# Create your own logger instance
custom_logger = Logger(structlog.get_logger("my-service"))
custom_logger.add_context(service="my-service")
custom_logger.info("Custom logger message")
Output Format
All logs are formatted as JSON with the following structure:
{
"event": "Your log message",
"level": "info",
"timestamp": "2024-01-01T12:00:00.000000Z",
"custom_field": "custom_value",
"context_field": "context_value"
}
Environment Configuration
Set the log level using environment variables:
# In your shell or .env file
export pyla_logger_level=warning
# In Docker
ENV pyla_logger_level=info
# In Python code (not recommended, use env vars instead)
import os
os.environ['pyla_logger_level'] = 'error'
Requirements
- Python 3.12+
- structlog >= 25.4.0
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
Version 1.2.0
- Environment-based log level configuration
- Context management functionality
- Exception logging with stack traces
- JSON structured output format
- Built on structlog foundation
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 pyla_logger-1.0.1rc1.tar.gz.
File metadata
- Download URL: pyla_logger-1.0.1rc1.tar.gz
- Upload date:
- Size: 23.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92c705dd64d4ed9d535efeece273cfa59bdbe8b48c55f8a65889735e1700ff01
|
|
| MD5 |
4c07118f1cc7bb786b17c454612fd964
|
|
| BLAKE2b-256 |
c436d41fe7b56a8b47a238a3a3c6bb9c14e9a70aa0a1ab71d97d39a954324973
|
Provenance
The following attestation bundles were made for pyla_logger-1.0.1rc1.tar.gz:
Publisher:
pypi.yml on langadventurellc/pyla-logger
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyla_logger-1.0.1rc1.tar.gz -
Subject digest:
92c705dd64d4ed9d535efeece273cfa59bdbe8b48c55f8a65889735e1700ff01 - Sigstore transparency entry: 362922262
- Sigstore integration time:
-
Permalink:
langadventurellc/pyla-logger@df16cdeb81ab37a60bf8d38708fc39d069fcab8e -
Branch / Tag:
refs/tags/v1.0.1rc1 - Owner: https://github.com/langadventurellc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@df16cdeb81ab37a60bf8d38708fc39d069fcab8e -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyla_logger-1.0.1rc1-py3-none-any.whl.
File metadata
- Download URL: pyla_logger-1.0.1rc1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e858854746ed77a94aaca5761399b76f763d418c7344dee41ff137baba54b8b
|
|
| MD5 |
b64cf3cbf3535fa5fc0548dd11db53c3
|
|
| BLAKE2b-256 |
eeac3b09f49b0c27d2a1c23870b16e5713d6d178f756ed62d561256d41cce172
|
Provenance
The following attestation bundles were made for pyla_logger-1.0.1rc1-py3-none-any.whl:
Publisher:
pypi.yml on langadventurellc/pyla-logger
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyla_logger-1.0.1rc1-py3-none-any.whl -
Subject digest:
5e858854746ed77a94aaca5761399b76f763d418c7344dee41ff137baba54b8b - Sigstore transparency entry: 362922272
- Sigstore integration time:
-
Permalink:
langadventurellc/pyla-logger@df16cdeb81ab37a60bf8d38708fc39d069fcab8e -
Branch / Tag:
refs/tags/v1.0.1rc1 - Owner: https://github.com/langadventurellc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@df16cdeb81ab37a60bf8d38708fc39d069fcab8e -
Trigger Event:
push
-
Statement type: