Skip to main content

Generic logging utilities for Python projects

Project description

ALT-logging-utils

Generic logging utilities for consistent log formatting across Python projects.

PyPI version Python Support License: MIT Tests codecov Documentation Status

Overview

ALT-logging-utils provides a collection of reusable logging functions to reduce duplication and maintain consistency across Python projects. These utilities help format log messages in a structured and readable way.

Features

  • Test Logging: Log test starts and ends with clear visual separators
  • File Operations: Log file saves, reads, and errors with consistent formatting
  • Component Lifecycle: Log initialization and configuration of components
  • Error Context: Log errors with additional context information
  • Operation Status: Log operation status with automatic level selection
  • Debug Values: Log debug values with optional prefixes
  • Collection Operations: Log collection completion with item counts

Installation

pip install ALT-logging-utils

Quick Start

import logging
from pathlib import Path
from alt_logging_utils import (
    log_test_start,
    log_test_end,
    log_saved_file,
    log_error_with_context,
    log_operation_status,
)

# Set up your logger
logger = logging.getLogger(__name__)

# Log test execution
log_test_start(logger, "test_user_authentication", "AuthenticationModule")
# ... test code ...
log_test_end(logger, "test_user_authentication")

# Log file operations
log_saved_file(logger, "configuration", Path("/etc/app/config.yml"))

# Log errors with context
try:
    result = risky_operation()
except Exception as e:
    log_error_with_context(
        logger, 
        e, 
        "processing user request",
        user_id=12345,
        operation="data_sync"
    )

# Log operation status
log_operation_status(logger, "Database backup", "completed", "5GB in 2 minutes")

API Reference

Test Logging

log_test_start(logger, test_name, test_class=None)

Logs the start of a test with visual separators.

log_test_start(logger, "test_login", "UserAuthTests")

log_test_end(logger, test_name)

Logs the end of a test.

log_test_end(logger, "test_login")

File Operations

log_saved_file(logger, file_type, filepath, level=logging.DEBUG)

Logs that a file has been saved.

log_saved_file(logger, "report", Path("./reports/monthly.pdf"), level=logging.INFO)

log_found_file(logger, file_type, filepath, level=logging.DEBUG)

Logs that a file was found.

log_found_file(logger, "config file", Path("/etc/app/config.yml"))

log_file_operation_error(logger, operation, filepath, error, level=logging.ERROR)

Logs a file operation error.

try:
    content = file.read()
except IOError as e:
    log_file_operation_error(logger, "read", Path("data.json"), e)

Component Lifecycle

log_initialization(logger, component, details=None)

Logs component initialization.

log_initialization(logger, "Database Connection", "postgres://localhost:5432/mydb")

log_configuration(logger, component, **config_items)

Logs component configuration with key-value pairs.

log_configuration(
    logger, 
    "API Client",
    base_url="https://api.example.com",
    timeout=30,
    retry_count=3
)

Error Handling

log_error_with_context(logger, error, context, **extra_info)

Logs an error with contextual information.

log_error_with_context(
    logger,
    exception,
    "processing payment",
    user_id=user.id,
    amount=150.00,
    currency="USD"
)

Status and Progress

log_operation_status(logger, operation, status, details=None)

Logs operation status with automatic level selection based on status.

log_operation_status(logger, "Data sync", "completed", "1000 records processed")
log_operation_status(logger, "Connection", "failed", "timeout after 30s")

log_collection_completed(logger, item_type, count)

Logs completion of a collection operation.

log_collection_completed(logger, "Users", 42)

Debug Helpers

log_debug_value(logger, name, value, prefix="")

Logs a debug value with consistent formatting.

log_debug_value(logger, "cache_size", 1024)
log_debug_value(logger, "requests", 42, prefix="Stats: ")

Constants

The package exports formatting constants that can be used in your own logging:

from alt_logging_utils import (
    LOG_SEPARATOR_LENGTH,    # Length of separator lines (60)
    LOG_SEPARATOR_CHAR,      # Character for major separators ('=')
    LOG_SUBSEPARATOR_CHAR,   # Character for minor separators ('-')
)

# Use in your own logging
logger.info("=" * LOG_SEPARATOR_LENGTH)

Best Practices

  1. Consistent Formatting: Use these utilities throughout your project for consistent log formatting
  2. Appropriate Levels: Use the level parameter to control log verbosity
  3. Rich Context: Provide meaningful context with errors using log_error_with_context
  4. Structured Data: Use log_configuration to log configuration in a structured way

Requirements

  • Python 3.8 or higher
  • No external dependencies (uses only Python standard library)

Documentation

Full documentation is available at:

Development

Setting Up Development Environment

# Clone the repository
git clone https://github.com/avilayani/ALT-logging-utils.git
cd ALT-logging-utils

# Set up development environment
make setup

# Or manually:
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"

Running Tests

# Run all tests with coverage
make test

# Run specific tests
pytest tests/test_logging_utils.py

# Run with coverage report
pytest --cov=alt_logging_utils --cov-report=html

Code Quality

# Run all quality checks
make all

# Individual checks
make lint        # Run linting
make format      # Format code
make type-check  # Type checking

Building Documentation

# Build HTML documentation
make docs

# Serve documentation locally
make docs-live

Contributing

We welcome contributions! Please see our Contributing Guide for details on:

  • Code style and standards
  • Development workflow
  • Submitting pull requests
  • Reporting issues

Roadmap

  • Add structured logging support (JSON output)
  • Add async logging utilities
  • Add performance metrics logging
  • Add log aggregation helpers
  • Add more customization options

Support

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Thanks to all contributors who have helped improve this package
  • Inspired by the need for consistent logging across multiple projects

Author

Avi Layani

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

alt_logging_utils-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

alt_logging_utils-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file alt_logging_utils-0.1.0.tar.gz.

File metadata

  • Download URL: alt_logging_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for alt_logging_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a456f1d99915bf8511b900e5a73d5c60f61aa2b1ed607354fa9204af716a3198
MD5 55ecc9ea25eca1c01d3c948d66cc3aad
BLAKE2b-256 41b959b332806df2cf931873fb89dd1c1e16a7a00be3c402c1576b116bb3b2fe

See more details on using hashes here.

File details

Details for the file alt_logging_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alt_logging_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 393def8fab298be68efe8c4fa0ac9bc9ef948c9ced0387f7f63e8aeee391a795
MD5 b960f69c342a49f1e45457816ce2bc3c
BLAKE2b-256 2ad8275e9b6781b560611ee9967ea42de1f2c960ec2c8eee50c19ec2ca3e72a7

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