Skip to main content
https://travis-ci.org/AFriemann/mdc.svg?branch=master

This is thought to be an easy to use, import and go, library for Mapped Diagnostic Context style logging.

Logs should include all necessary fields collected by the python logging library. Helper methods are provided to add context fields where required.

NOTE 1.2.0: recent changes will result in changed json output if using the MDCHandler class:

>>> with MDC(foobar="baz"):
...   logging.warning("test")
{ "message": "test", "foobar": "baz" }

this is a temporary solution to avoid breaking existing APIs. Please excuse any issues this might cause. In the future output handling should be done on the client side to avoid any possible breakage. The MDCHandler class will eventually be removed.

Installation

PyPi:

$ pip install --user mdc

From source:

$ pip install --user .

Usage

Add a handler to the root logger or set the base handler with logging.basicConfig:

>>> import logging
>>> from mdc import MDCHandler

>>> # use the MDCHandler only
>>> logging.basicConfig(level=logging.DEBUG, handlers=[MDCHandler()])

>>> # the MDC contextmanager
>>> with MDC(foo='bar'):
...     logging.warning('foobar')
{ ..., "mdc": { "foo": "bar" }, "extra": {}, ... }

>>> # the with_mdc decorator
>>> @with_mdc(test='123')
... def foobar(ctx):
...   logging.warning('some warning')

>>> foobar()
{ ..., "mdc": { "test": "123" }, "extra": {}, ... }

>>> # logging with extra fields
>>> logging.error('some error', extra=dict(foo='bar'))
{ ..., "mdc": {}, "extra": { "foo": "bar" }, ... }

By default log messages will include the following fields:

{
  "message": "deleting context b8321b4f-19ff-4c98-b011-5a97178e7ad6",
  "logger": "mdc",
  "timestamp": "2018-03-07T21:39:12.010851",
  "level": "DEBUG",
  "mdc": {
    "foo": "bar",
    "index": 76
  },
  "extra": {},
  "python": {
    "module": "__init__",
    "function": "MDC",
    "path": "/home/user/git/mdc/mdc/__init__.py",
    "file": "__init__.py",
    "line": 56,
    "process": {
      "name": "MainProcess",
      "id": 31678
    },
    "thread": {
      "name": "MainThread",
      "id": 140433289192768
    }
  }
}

Running tests:

$ tox

Metadata

Release files for mdc 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mdc 1.2.1
File Size Uploaded
mdc-1.2.1.tar.gz 5.3 kB Details

Release files / mdc-1.2.1.tar.gz

Download URL mdc-1.2.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
deb9df228fc89ed185f3df4c00ca2aeb76a28ad5a6bb7b98bab413de0f44779e
BLAKE2b-256 checksum
How to use checksums
6367e853e9d5617b46ece35f3789561a2b4ddbe5941e6d840b456e600ff72b45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.4.8

Release history Release notifications | RSS feed

This release

1.2.1 This release

1 release file

1.2.0

2 release files

1.1.0

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page