Skip to main content

A comprehensive logging library with OpenTelemetry support, environment variable handling, and advanced log rotation

Project description

HD Logging

A comprehensive Python logging library with OpenTelemetry support, environment variable handling, and advanced log rotation capabilities.

Features

  • 🎨 Colorized Console Output - Beautiful, color-coded log messages
  • 📊 OpenTelemetry Integration - JSON format logging with rich metadata
  • 🔒 Environment Variable Security - Automatic sensitive data masking
  • 📁 Advanced Log Rotation - Size and time-based rotation with compression
  • ⚙️ Flexible Configuration - Environment variables and programmatic setup
  • 🚀 High Performance - Optimized for production workloads
  • 🔧 Easy Integration - Simple setup with powerful features

Installation

Using pip

pip install hd-logging

Using uv (recommended)

uv add hd-logging

Development Installation

git clone https://github.com/tejaswiredkar/hd-logging.git
cd hd-logging
uv sync
uv pip install -e .

Quick Start

Basic Usage

from hd_logging import setup_logger

# Create a logger with default settings
logger = setup_logger("my_app")

# Log messages
logger.info("Application started")
logger.warning("This is a warning")
logger.error("An error occurred")

OpenTelemetry Format

from hd_logging import setup_logger

# Create a logger with OpenTelemetry JSON format
logger = setup_logger(
    "my_service",
    use_otlp_format=True,
    service_name="my-service",
    environment="production",
    service_version="1.0.0",
    log_file_path="logs/service.log"
)

# Log with custom attributes
logger.info("User action performed", extra={
    "user_id": "12345",
    "action": "login",
    "ip_address": "192.168.1.1"
})

Environment Variable Integration

from hd_logging import setup_logger, load_env_file

# Load environment variables from .env file
load_env_file()

# Logger will automatically use environment variables
logger = setup_logger("env_configured")

Configuration

Environment Variables

The library supports configuration through environment variables:

# Log levels
LOG_LEVEL=INFO                    # Console and file log level
LOG_FILE_OTLP_FORMAT=true         # Enable OpenTelemetry format

# Service information
SERVICE_NAME=my-service           # Service name for OTLP logs
ENVIRONMENT=production            # Environment name
SERVICE_VERSION=1.0.0            # Service version

# Log file settings
LOG_FILE=logs/app.log             # Log file path

Programmatic Configuration

from hd_logging import setup_logger
import logging

logger = setup_logger(
    logger_name="my_app",
    log_file_path="logs/app.log",
    log_level_console=logging.INFO,
    log_level_files=logging.DEBUG,
    use_otlp_format=True,
    service_name="my-service",
    environment="production",
    service_version="1.0.0"
)

Advanced Features

Log Rotation

The library includes advanced log rotation with both size and time-based rotation:

# Automatic rotation when:
# - File size exceeds 20MB (configurable)
# - Daily rotation at midnight
# - Automatic compression of rotated files
# - Retention of 7 days (configurable)

Sensitive Data Masking

Automatic masking of sensitive environment variables:

from hd_logging import log_env_vars_with_masking

# Logs environment variables with sensitive data masked
log_env_vars_with_masking()

Custom Attributes

Add rich metadata to your logs:

logger.info("Order processed", extra={
    "order_id": "ORD-12345",
    "customer_id": "CUST-67890",
    "amount": 99.99,
    "currency": "USD",
    "payment_method": "credit_card"
})

Examples

See the examples/ directory for comprehensive usage examples:

Run examples:

python examples/basic_usage.py
python examples/opentelemetry_usage.py
# ... and more

API Reference

setup_logger()

def setup_logger(
    logger_name: str,
    log_file_path: Optional[str] = None,
    log_level_console: Optional[int] = None,
    log_level_files: Optional[int] = None,
    use_otlp_format: bool = None,
    service_name: Optional[str] = None,
    environment: Optional[str] = None,
    service_version: Optional[str] = None
) -> logging.Logger

Parameters:

  • logger_name: Name of the logger
  • log_file_path: Path to log file (default: from LOG_FILE env var)
  • log_level_console: Console log level (default: from LOG_LEVEL env var)
  • log_level_files: File log level (default: from LOG_LEVEL env var)
  • use_otlp_format: Enable OpenTelemetry format (default: from LOG_FILE_OTLP_FORMAT env var)
  • service_name: Service name for OTLP logs (default: from SERVICE_NAME env var)
  • environment: Environment name (default: from ENVIRONMENT env var)
  • service_version: Service version (default: from SERVICE_VERSION env var)

Environment Variable Functions

from hd_logging import (
    load_env_file,           # Load .env file
    find_env_file,           # Find .env file path
    get_env_file_path,       # Get .env file path
    log_env_vars_with_masking,  # Log env vars with masking
    log_dotenv_vars_with_masking,  # Log .env vars with masking
    get_env_vars_with_masking,    # Get env vars with masking
    get_dotenv_vars_with_masking  # Get .env vars with masking
)

Log Formats

Standard Format

2024-01-15T10:30:45Z - my_app - INFO - Application started - [Component: main, Function: main, Line: 15]

OpenTelemetry JSON Format

{
  "timestamp": "2024-01-15T10:30:45.123456Z",
  "severityText": "INFO",
  "body": "Application started",
  "attributes": {
    "service.name": "my-service",
    "environment": "production",
    "logger.name": "my_app",
    "component": "main",
    "function": "main",
    "line": 15
  },
  "resource": {
    "host.name": "server-01",
    "os.type": "linux",
    "service.name": "my-service",
    "service.version": "1.0.0",
    "service.instance.id": "01HZ1234567890ABCDEF",
    "environment": "production"
  }
}

Requirements

  • Python 3.8+
  • colorlog >= 6.9.0
  • python-dotenv >= 1.0.0
  • ulid-py >= 1.1.0

Development

Setup Development Environment

git clone https://github.com/tejaswiredkar/hd-logging.git
cd hd-logging
uv sync

Run Tests

uv run pytest

Code Formatting

uv run black src/
uv run flake8 src/
uv run mypy src/

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. Submit a pull request

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Support

Changelog

See CHANGELOG.md for a history of changes.


Made with ❤️ by Hackerdogs.ai

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

hd_logging-1.0.4.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

hd_logging-1.0.4-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file hd_logging-1.0.4.tar.gz.

File metadata

  • Download URL: hd_logging-1.0.4.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for hd_logging-1.0.4.tar.gz
Algorithm Hash digest
SHA256 1afc0395b9ed1d6fa2e5b588302dcca9213be0c020bfa51a4b80b11b0e38fc9a
MD5 b09e569322bf6a7f7932f9238b218055
BLAKE2b-256 f8b1e7ede5ff630bd4ab2887f2752d3367f3a391dfe8b1096f08fb9e5fc34260

See more details on using hashes here.

File details

Details for the file hd_logging-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: hd_logging-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for hd_logging-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 10772169dccf4e3b99ea3b4974770fa02f34845542d3cbc7d34132cbf1920e3e
MD5 222ddcd9c4df032971e7c5a89b433540
BLAKE2b-256 99a6372977b5bb595151c3dacad7d04bd57ecdb152e95fecaab4c19a144b3800

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