Skip to main content

Mapped Diagnostic Context (MDC) library for python

Project description

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.

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

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

mdc-1.2.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

mdc-1.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file mdc-1.2.0.tar.gz.

File metadata

  • Download URL: mdc-1.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.4.8

File hashes

Hashes for mdc-1.2.0.tar.gz
Algorithm Hash digest
SHA256 cbdf716cf093b150253373439b4528a27be29ae6196fba507e2b77e993b89f77
MD5 92c4ad36ea8e78e6591922a3b6eb82f8
BLAKE2b-256 867c4df85b3589c0c558d22193755deee0512814b7df04f221fbdf947c4a6add

See more details on using hashes here.

File details

Details for the file mdc-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: mdc-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for mdc-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fd80830a375968005e3515f6953551e1f63d0cb6ce5ed0b9f45cb586353373e
MD5 d6e06336fd06ca12e27e9ba60e540381
BLAKE2b-256 e540528804fa77956388c2fb5f3d7ddff7e90b3f262ae69a1d0a415385496c47

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