A Python package from the ds-common library collection
Project description
ds-common-logger-py-lib
A Python logging library from the ds-common library collection, providing structured logging with support for extra fields, class-based loggers, and flexible configuration.
Installation
Install the package using pip:
pip install ds-common-logger-py-lib
Or using uv (recommended):
uv pip install ds-common-logger-py-lib
Features
- Structured Logging: Built-in support for extra fields in log messages
- Class-Based Loggers:
LoggingMixinprovides automatic logger setup for classes - Per-Class Isolation: Each class gets its own logger instance with distinct names
- Application Configuration:
LoggerConfigfor application-level prefix and format setup - Flexible Configuration: Set log levels at class level, instance level, or per-call
- Customizable Format: Override default log format using
set_log_format()method - Custom Formatter: Includes extra fields in JSON format for better log parsing
- Standard Library Compatible: Built on Python's
loggingmodule
Quick Start
Basic Usage
from ds_common_logger_py_lib import Logger
# Initialize logger configuration
Logger()
# Get a logger instance
logger = Logger.get_logger(__name__)
# Log with extra fields
logger.info("Processing data", extra={"user_id": 123, "action": "process"})
Using LoggingMixin
from ds_common_logger_py_lib import LoggingMixin
import logging
class UserService(LoggingMixin):
log_level = logging.INFO
def create_user(self, username: str) -> dict:
self.log.info("Creating user", extra={"username": username})
return {"id": 1, "username": username}
# Use the service
service = UserService()
service.create_user("alice")
Application Configuration
from ds_common_logger_py_lib import LoggerConfig, Logger
import logging
# Configure at application startup
LoggerConfig.configure(
prefix="MyApp",
format_string="[%(asctime)s][{prefix}][%(name)s][%(levelname)s]: %(message)s",
level=logging.INFO
)
logger = Logger.get_logger(__name__)
logger.info("Application started")
# Update prefix at runtime
LoggerConfig.set_prefix("MyApp-session123")
logger.info("Session initialized")
Usage Examples
Setting Log Levels
from ds_common_logger_py_lib import LoggingMixin
import logging
class MyService(LoggingMixin):
log_level = logging.DEBUG # Set default level for the class
def process(self):
self.log.debug("Debug message")
self.log.info("Info message")
# Or change level at runtime
MyService.set_log_level(logging.WARNING)
# Or override per call
logger = MyService.logger(level=logging.DEBUG)
logger.debug("This will be shown")
Multiple Classes with Isolated Loggers
from ds_common_logger_py_lib import LoggingMixin
class UserService(LoggingMixin):
def create_user(self, username: str):
self.log.info("Creating user", extra={"username": username})
class OrderService(LoggingMixin):
def create_order(self, user_id: int):
self.log.info("Creating order", extra={"user_id": user_id})
# Each class has its own logger with distinct names
user_service = UserService()
order_service = OrderService()
Requirements
- Python 3.10 or higher
Documentation
Full documentation is available at:
Development
To contribute or set up a development environment:
# Clone the repository
git clone https://github.com/grasp-labs/ds-common-logger-py-lib.git
cd ds-common-logger-py-lib
# Install development dependencies
uv sync --all-extras --dev
# Run tests
make test
See the README for more information.
License
This package is licensed under the Apache License 2.0. See the LICENSE-APACHE file for details.
Support
- Issues: GitHub Issues
- Releases: GitHub Releases
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 ds_common_logger_py_lib-0.1.0a4.tar.gz.
File metadata
- Download URL: ds_common_logger_py_lib-0.1.0a4.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34860caff7cb48840c03164ef529b1538151c8749a3d44d64e323e1f960ba9f4
|
|
| MD5 |
0124bc55c6b7fc0d6eea23865e311de3
|
|
| BLAKE2b-256 |
6377db8bd5ebc7d288fbfe645d0eadba8b1d59f902e98f99ad8a338ce6cd84bb
|
File details
Details for the file ds_common_logger_py_lib-0.1.0a4-py3-none-any.whl.
File metadata
- Download URL: ds_common_logger_py_lib-0.1.0a4-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f63326d9485e9c93e29d1bb4264bfe9ffcb432a349847cd9df7e55a4a0b7c340
|
|
| MD5 |
d057ced75981d53c5b814de63051a49e
|
|
| BLAKE2b-256 |
d404c74469e4f63cfa181e0e71eae1171a7bb927987c2654672ec5c54cbd5010
|