Registry / observability / mdc
library1.2.1pypypi✓ verified 85d ago

A Python implementation of Mapped Diagnostic Context (MDC) for structured logging. MDC allows you to enrich log records with contextual key-value pairs, propagate context across threads and async code, and format logs as JSON. Current version 1.2.1, released on PyPI, with irregular releases.

pip install mdc
INSTALL
IMPORT
SIG · MDC
M
mdc
observabilitypythonv1.2.1
Install
2.7s avg
Import
73ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.076s · 23.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.7s · import 0.070s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MDC
from mdc import MDC
MDCFormatter
from mdc import MDCFormatter
MDCHandler
from mdc import MDCHandler
Legacy class kept for compatibility

Basic usage: use MDC as a context manager to add key-value pairs to log records.

from mdc import MDC import logging # Set up logging handler = logging.StreamHandler() handler.setFormatter(MDCFormatter()) logging.getLogger().addHandler(handler) logging.getLogger().setLevel(logging.INFO) # Use MDC context with MDC(user_id='1234', ip='127.0.0.1'): logging.info('Processing request') # Output includes MDC fields
mdc --version
Debug
Known issues
breakingIn version 1.2.0, the MDC context was changed to support generators and argument catching. If you used MDC in older versions, ensure you are using the latest API.
fix
Upgrade to >=1.2.0 and review your usage of MDC as a decorator or context manager.
affects: <1.2.0
gotchaMDC context does NOT automatically propagate to child threads or async tasks unless you explicitly pass the context. Use MDC.copy() or MDC.get_context() to pass it manually.
fix
When spawning threads or tasks, manually copy the MDC context: context = MDC.get_context(); # pass to thread/task
affects: all
deprecatedMDCHandler is a legacy class kept for backward compatibility but may be removed in future versions. Use MDCFormatter with a standard logging Handler instead.
fix
Use from mdc import MDC, MDCFormatter and configure logging with MDCFormatter on any Handler.
affects: >=1.2.0
gotchaWhen using MDC as a decorator on generator functions, context is lost after the first yield. Version 1.2.1 fixed this bug, but older versions (<1.2.1) had this issue.
fix
Upgrade to 1.2.1 or later to preserve context throughout generator execution.
affects: <1.2.1
Errors
Common errors & fixes
ImportError: cannot import name 'MDCFormatter' from 'mdc'
You are using an older version of mdc that doesn't include MDCFormatter.
fix
Install the latest version: pip install --upgrade mdc
TypeError: 'int' object is not iterable
MDC expects values to be strings or objects convertible to string. Passing non-string values may cause formatting errors.
fix
Convert values to strings before passing to MDC: MDC(user_id=str(1234))
AttributeError: module 'mdc' has no attribute 'MDC'
Possibly installed a different package named mdc, or very old version.
fix
Run pip install --upgrade mdc. If problem persists, check your environment for conflicting mdc installations.
Upgrade
Version history
1.2.1latest on PyPI · released Sep 9, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
mdc — pip install mdc · libregistry