Skip to main content

Logging utilities for Python applications

Project description

MTK Logger

Logging utilities for MarkTrack applications.

Installation

To install from GitHub Packages:

# Configure pip to use GitHub Packages
pip config set global.extra-index-url https://YOUR_GITHUB_TOKEN@github.com/pprasquier/mtk_logger/raw/main/dist/

# Install the package
pip install mtk_logger

Or with Poetry:

# Add GitHub Packages as a source
poetry source add --priority=supplemental github https://github.com/pprasquier/mtk_logger/raw/main/dist/

# Install the package
poetry add mtk_logger

Usage

Default import (optional settings_dir; defaults to "settings_files" for backwards compatibility):

from mtk_logger import get_logger

logger = get_logger(__name__)
logger.info("This is a log message")

Custom Prefy settings directory (same pattern as Preferences(settings_dir) elsewhere):

from mtk_logger import get_logger

logger = get_logger(__name__, settings_dir="backend/settings/app")

Structured JSON logs (log_format = json in Prefy) include any keys you set on CorrelationContext, plus fields passed via stdlib extra= (0.7.0+):

from mtk_logger import CorrelationContext, get_logger

logger = get_logger(__name__, settings_dir="path/to/settings")
CorrelationContext.set("cycle_id", "cyc-abc")
CorrelationContext.set("job_id", "job-123")  # vendor-neutral: any string keys
logger.info(
    "doing work",
    extra={"event": "pipeline.cycle.created", "latency_ms": 42},
)

The JSON event field uses extra["event"] when provided; otherwise it falls back to the logger name (0.6.x behaviour). CorrelationContext values override the same key on extra=.

Exports: MTKLogger, get_logger, CorrelationContext, JSONFormatter.

FastAPI middleware

from mtk_logger.fastapi_middleware import RequestLoggingMiddleware

app.add_middleware(RequestLoggingMiddleware)

With a custom settings directory (matches get_logger(..., settings_dir=...)):

from mtk_logger.fastapi_middleware import RequestLoggingMiddleware

app.add_middleware(RequestLoggingMiddleware, settings_dir="backend/settings/app")

RequestLoggingMiddleware sets request_id on CorrelationContext for the request lifetime (restored afterward) and emits latency_ms on the completion log via extra=. Legacy message text is unchanged.

See settings_example.json for Prefy keys: log_dir, log_format, process_name, etc.

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

mtk_logger-0.8.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

mtk_logger-0.8.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file mtk_logger-0.8.0.tar.gz.

File metadata

  • Download URL: mtk_logger-0.8.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mtk_logger-0.8.0.tar.gz
Algorithm Hash digest
SHA256 8c653f87cff3e86d8dae261a698b069eb6a59f783c7a41f5920d47184a8b7829
MD5 a05701c6ccd360b78574d8495720b560
BLAKE2b-256 80f0e5daba4750ee216c096e530202560767a8654ad7b660357d14934ab268ca

See more details on using hashes here.

File details

Details for the file mtk_logger-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: mtk_logger-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mtk_logger-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3e3deec7853f8f8feba51c188b2637265fc88d684711539a09cadcdf47191ee
MD5 f87195882903912b1028640894b5f79b
BLAKE2b-256 8d80aa9df9ba82eabb3fe4355bbf3557164cc21f59d1c576796608f6b93abc6d

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