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.

0.9.0 migration note

The public constructor signature and log events are unchanged. Only the implementation moved from Starlette BaseHTTPMiddleware to pure ASGI (async def __call__(self, scope, receive, send)), which avoids deadlocks when stacked with other pure-ASGI middleware under httpx.ASGITransport / TestClient. No application code changes are required beyond bumping the dependency.

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.9.0.tar.gz (7.5 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.9.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mtk_logger-0.9.0.tar.gz
  • Upload date:
  • Size: 7.5 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.9.0.tar.gz
Algorithm Hash digest
SHA256 59914afd00e13edc99c79268777ca51585d9ea49897e1b77d1a40b5212d23ff5
MD5 1d0238b31b3da1cefc6b9f10720f9c49
BLAKE2b-256 627adfbdfba31f1d4757daaf0d945fb723ad8d944964fffe60bec95ed7777a3a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mtk_logger-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 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.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83bcc01417fe79397467efee65d999ead2471abb2da18ee6b74ba774f2840700
MD5 edd230cbd46c73bfd791664aceab8389
BLAKE2b-256 aa66616a49dec6d9bb3eb3009eb220aa87f94f644862e2e2410e0943110a0d22

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