Skip to main content

Batteries-included logging for Python applications.

Project description

macwinnie-enhanced-logging

Last built version: v0.1.1

Batteries-included logging for Python applications.

macwinnie-enhanced-logging provides a clean, consistent logging setup built on top of:

  • Python’s built-in logging module (so third-party libraries keep working normally)
  • structlog (so your logs are structured, predictable, and easy to analyze)

It supports both developer-friendly console logs and structured JSON logs suitable for production environments like Kubernetes.


Features

  • Works with standard Python logging
  • Structured logging with structlog
  • Human-friendly console output for development
  • JSON output for log pipelines and observability stacks
  • Optional callsite information (file/function/line)
  • Built-in context variables (request_id, job_id, etc.)
  • Optional speaking logs for critical events
  • Zero boilerplate for most applications

Installation

pip install macwinnie-enhanced-logging

Requires Python 3.12+.


Quick Start

1. Configure logging once in your application entry point

from enhanced_logging import configure_logging, get_logger

configure_logging()

log = get_logger(__name__)
log.info("startup", _fmt="Starting {app}…", app="demo")

2. Use the logger anywhere in your code

from enhanced_logging import get_logger

log = get_logger(__name__)

log.info("something_happened", user_id=123)

Output Formats

Two output formats are supported.

Human format (default)

Developer-friendly console output:

2025-01-01T10:00:00Z [info] Starting api on 127.0.0.1:8080 component=api host=127.0.0.1 port=8080

JSON format

Machine-friendly structured output:

{
  "timestamp": "2025-01-01T10:00:00Z",
  "level": "info",
  "event": "startup",
  "msg": "Starting api on 127.0.0.1:8080",
  "component": "api",
  "host": "127.0.0.1",
  "port": 8080
}

Switch formats using an environment variable:

LOG_FORMAT=json

_fmt: Human-Readable Messages with Structured Logs

You can provide _fmt to generate a human-readable message while still keeping structured fields.

Example:

log.info(
    "startup",
    _fmt="Starting {component} on {host}:{port}",
    component="api",
    host="127.0.0.1",
    port=8080,
)

Behavior:

  • human format → replaces the event message
  • json format → keeps event and adds msg

This allows you to combine structured logging with readable messages.


Context Variables

You can attach fields to all logs within a scope using log_context.

Example:

from enhanced_logging import get_logger, log_context

log = get_logger(__name__)

with log_context(request_id="req-123", user_id=42):
    log.info("handling_request")
    do_work()
    log.info("done")

All logs inside the block will automatically include:

request_id=req-123 user_id=42

This is ideal for:

  • request IDs
  • job IDs
  • tenant identifiers
  • correlation IDs

Environment Variables

Configuration is primarily done through environment variables.

Variable Description Default
LOG_FORMAT human or json human
LOG_LEVEL Logging level INFO
LOG_UTC Use UTC timestamps 1
LOG_KEEP_HANDLERS Keep existing root handlers 0
LOG_CALLSITE auto, 1, or 0 auto
LOG_APP_NAME Optional application name unset
LOG_SPEAK Enable spoken logs 0
LOG_SPEAK_LEVEL Minimum level to speak ERROR
LOG_SPEAK_CMD Custom speech command auto

Callsite Information

Callsite fields include:

  • filename
  • function name
  • line number

Behavior:

  • Automatically enabled when LOG_LEVEL=DEBUG
  • Can be forced via:
LOG_CALLSITE=1

Disable explicitly:

LOG_CALLSITE=0

Speaking Logs (Optional)

macwinnie-enhanced-logging can optionally speak important log messages aloud.

This can be useful for:

  • CI pipelines
  • long-running batch jobs
  • alerting during development

Enable speaking:

LOG_SPEAK=1

Only errors and above are spoken by default.

Change threshold:

LOG_SPEAK_LEVEL=WARNING

Platform support:

Platform Engine
macOS say
Linux espeak
Windows PowerShell SAPI

If no speech engine is available, speaking is silently disabled.

You can also call the helper directly:

from enhanced_logging import speak

speak("Deployment finished successfully")

Configuration Example

from enhanced_logging import configure_logging, LogConfig

configure_logging(
    LogConfig(
        format="json",
        level="INFO",
        app_name="my-service"
    )
)

Integration with Third-Party Libraries

Because macwinnie-enhanced-logging configures the root logger, libraries that use:

logging.getLogger(...)

will automatically emit logs through the same handler.

This means:

  • one logging configuration for your entire application
  • consistent formatting across libraries

Best Practices

  • Call configure_logging() once at application startup
  • Use get_logger(__name__) in each module
  • Prefer structured fields over string concatenation
  • Use _fmt when you want readable console output

Example:

log.info(
    "user_login",
    _fmt="User {user_id} logged in",
    user_id=123
)

Acknowledgements

Author of this package is @macwinnie.

This project builds on:

which provide the foundation for structured logging in Python.

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

macwinnie_enhanced_logging-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

macwinnie_enhanced_logging-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file macwinnie_enhanced_logging-0.1.1.tar.gz.

File metadata

File hashes

Hashes for macwinnie_enhanced_logging-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b7c91c8445000f6655557123b5b1d177f9abecdc2600fa3fa181cb966e56e24f
MD5 6a2b4ba0f30f6cc094c4a9b8292d16b7
BLAKE2b-256 c4e6bb56ef3b2ecd2d1d2a731db03571dcc7f17ac900311a62f8b768fc1ea138

See more details on using hashes here.

Provenance

The following attestation bundles were made for macwinnie_enhanced_logging-0.1.1.tar.gz:

Publisher: publish.yml on macwinnie/py-enhanced-logging

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file macwinnie_enhanced_logging-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for macwinnie_enhanced_logging-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df072310cf944861565b94c068c53b2090cfe409b55851812e9f9c87af8ac09b
MD5 ca32b5414de98ad90fa3a3c83a72de65
BLAKE2b-256 1be32def8da820c7d15faac6f7cad6a81fca63ab289c3285f6c6ba91d3b8d7fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for macwinnie_enhanced_logging-0.1.1-py3-none-any.whl:

Publisher: publish.yml on macwinnie/py-enhanced-logging

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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