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

>>> logging.basicConfig(level=logging.DEBUG, handlers=[MDCHandler()])

By default log messages will include the following fields:

{
  "message": "...",
  "logger": "...",
  "timestamp": "2018-02-17T16:39:53.475377",
  "level": "DEBUG",
  "mdc": {},
  "python": {
    "module": "...",
    "function": "...",
    "path": "...",
    "line": 0,
    "process": {
      "name": "MainProcess",
      "pid": 3724
    },
    "thread": {
      "name": "MainThread",
      "tid": 140050978850112
    }
  }
}

You can use the provided decorator or contextmanager to add MDC fields:

>>> from mdc import MDC, with_mdc

>>> with MDC(foo='bar'):
...     logging.warning('foobar')

>>> @with_mdc(test='123')
... def foobar(ctx):
...   pass

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.0.7.tar.gz (3.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: mdc-1.0.7.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mdc-1.0.7.tar.gz
Algorithm Hash digest
SHA256 4b5e168e088ad67f1d673e80b62d0e06c0e752907dfb8cca230742a9af7fc774
MD5 4815093da42ea3e8a188d0082efebe8f
BLAKE2b-256 a1221f4c8b1af568ef89d9efb49a1f71369efa2b2f43bffd3ad18452a92066da

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