{"id":21559,"library":"mdc","title":"MDC (Mapped Diagnostic Context)","description":"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.","status":"active","version":"1.2.1","language":"python","source_language":"en","source_url":"https://github.com/afriemann/mdc","tags":["logging","context","mdc","structured-logging"],"install":[{"cmd":"pip install mdc","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"","wrong":"","symbol":"MDC","correct":"from mdc import MDC"},{"note":"","wrong":"","symbol":"MDCFormatter","correct":"from mdc import MDCFormatter"},{"note":"Legacy class kept for compatibility","wrong":"","symbol":"MDCHandler","correct":"from mdc import MDCHandler"}],"quickstart":{"code":"from mdc import MDC\nimport logging\n\n# Set up logging\nhandler = logging.StreamHandler()\nhandler.setFormatter(MDCFormatter())\nlogging.getLogger().addHandler(handler)\nlogging.getLogger().setLevel(logging.INFO)\n\n# Use MDC context\nwith MDC(user_id='1234', ip='127.0.0.1'):\n    logging.info('Processing request')  # Output includes MDC fields\n","lang":"python","description":"Basic usage: use MDC as a context manager to add key-value pairs to log records."},"warnings":[{"fix":"Upgrade to >=1.2.0 and review your usage of MDC as a decorator or context manager.","message":"In 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.","severity":"breaking","affected_versions":"<1.2.0"},{"fix":"When spawning threads or tasks, manually copy the MDC context: context = MDC.get_context(); # pass to thread/task","message":"MDC 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.","severity":"gotcha","affected_versions":"all"},{"fix":"Use from mdc import MDC, MDCFormatter and configure logging with MDCFormatter on any Handler.","message":"MDCHandler is a legacy class kept for backward compatibility but may be removed in future versions. Use MDCFormatter with a standard logging Handler instead.","severity":"deprecated","affected_versions":">=1.2.0"},{"fix":"Upgrade to 1.2.1 or later to preserve context throughout generator execution.","message":"When 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.","severity":"gotcha","affected_versions":"<1.2.1"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Install the latest version: pip install --upgrade mdc","cause":"You are using an older version of mdc that doesn't include MDCFormatter.","error":"ImportError: cannot import name 'MDCFormatter' from 'mdc'"},{"fix":"Convert values to strings before passing to MDC: MDC(user_id=str(1234))","cause":"MDC expects values to be strings or objects convertible to string. Passing non-string values may cause formatting errors.","error":"TypeError: 'int' object is not iterable"},{"fix":"Run pip install --upgrade mdc. If problem persists, check your environment for conflicting mdc installations.","cause":"Possibly installed a different package named mdc, or very old version.","error":"AttributeError: module 'mdc' has no attribute 'MDC'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}