Skip to main content

Structured logging configuration with OTel and Sentry support

Project description

Python Structlog Config

Structured logging configuration with optional OpenTelemetry and Sentry integration.

Extracted from: GridFlow backend/src/infrastructure/logging_config.py

Installation

# Basic installation
pip install python-structlog-config

# With OTel support
pip install python-structlog-config[otel]

# With Sentry support
pip install python-structlog-config[sentry]

# With all integrations
pip install python-structlog-config[all]

Features

  • Structured Logging: JSON or console output
  • OpenTelemetry: Optional OTel instrumentation
  • Sentry: Optional Sentry error tracking
  • Presets: Development, production, and testing configurations

Usage

Quick Start

from python_structlog_config import configure_for_development, get_logger

# Configure logging for development
configure_for_development("my-api")

# Get logger
logger = get_logger(__name__)
logger.info("Application started", version="1.0.0")

Production Setup

from python_structlog_config import configure_for_production

configure_for_production(
    "my-api",
    sentry_dsn="https://...@sentry.io/...",
    log_level="INFO"
)

Custom Configuration

from python_structlog_config import configure_structlog

configure_structlog(
    log_level="DEBUG",
    json_output=True,
    enable_otel=True,
    enable_sentry=True,
    sentry_dsn="https://...@sentry.io/...",
    service_name="my-api",
    environment="staging"
)

Testing Setup

from python_structlog_config import configure_for_testing

configure_for_testing("my-api")

Presets

Development

  • Log Level: DEBUG
  • Output: Console (human-readable)
  • OTel: Disabled
  • Sentry: Disabled

Production

  • Log Level: INFO
  • Output: JSON
  • OTel: Enabled
  • Sentry: Optional (if DSN provided)

Testing

  • Log Level: WARNING
  • Output: Console
  • OTel: Disabled
  • Sentry: Disabled

API Reference

configure_structlog(...)

Configure structlog with optional integrations.

Parameters:

  • log_level (str): Logging level (default: "INFO")
  • json_output (bool): Use JSON renderer (default: False)
  • enable_otel (bool): Enable OTel (default: False)
  • enable_sentry (bool): Enable Sentry (default: False)
  • sentry_dsn (Optional[str]): Sentry DSN
  • service_name (Optional[str]): Service name
  • environment (Optional[str]): Environment name

get_logger(name=None)

Get a structlog logger instance.

Parameters:

  • name (Optional[str]): Logger name

Returns:

  • Structlog logger instance

Presets

  • configure_for_development(service_name): Development preset
  • configure_for_production(service_name, sentry_dsn, log_level): Production preset
  • configure_for_testing(service_name): Testing preset

Integrations

OpenTelemetry

Requires: pip install python-structlog-config[otel]

configure_structlog(enable_otel=True, service_name="my-api")

Sentry

Requires: pip install python-structlog-config[sentry]

configure_structlog(
    enable_sentry=True,
    sentry_dsn="https://...@sentry.io/...",
    environment="production"
)

Dependencies

  • structlog>=23.0.0

Optional:

  • opentelemetry-instrumentation-logging>=0.40b0 (OTel)
  • sentry-sdk>=1.40.0 (Sentry)

License

MIT

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

python_structlog_config-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

python_structlog_config-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for python_structlog_config-0.1.0.tar.gz
Algorithm Hash digest
SHA256 056d07e59f9ea932a755c81129457d946ed7aafbcc4ff48506f17180650f865d
MD5 37b03a82c34070e104817a557d225466
BLAKE2b-256 10ac81dd63d4227d0c622968d0e1a827f9e3bff44e53ae3b3815291351d51261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_structlog_config-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24cc834e18e604e91e8289e75aa7998fcf288ee95e93a75f24a4503b989e4e57
MD5 70daea1c192fb7988b11cb3c569f25da
BLAKE2b-256 8622807bee771932fb1bccd6d3cedf579347710169be70c49bdc3b7e38c01413

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